Python appending to two lists when it should only append to one

后端 未结 3 882
半阙折子戏
半阙折子戏 2021-01-27 13:43

I have a list called teams which contains two objects, these are objects of the same class and they both have a \"members\" list. I am appending to these lists individually. See

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-27 14:45

    In all of your classes, you want to initialize instance variables like this:

    def __init__(self):
        self.participants = []
        self.teams = []
        self.attacked = []
        self.fighting = 0
    

    That way, they are separate for each fight, participant, team instead of shared for all fights, participants, or teams.

提交回复
热议问题