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
Post a Comment