python - Deep Copy - Objects with vectors -


i typed code bellow. expected john memeber of team_b only. when run code, john beeing added both teams, when use "deepcopy":

import copy  class team:     players = []  team_a = team()  team_a.players.append("tom") team_a.players.append("peter") team_a.players.append("mario")  team_b = copy.deepcopy(team_a) team_b.players.append("john") 

could explain , me fix it?

currently players class variable shared between all team objects, want each instance have it's own list of players.

class team:     def __init__(self):         self.players = [] 

the __init__ code run on object construction, note self keyword, refers current instance of team.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -