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

后端 未结 3 887
半阙折子戏
半阙折子戏 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:31

    You have made the lists as class attributes, which means the lists are shared by all instances. It's the same list. You should make the lists instance attributes. Do that by creating them in the __init__ (constructor) method of the class.

提交回复
热议问题