A private list variable is inadvertently shared between instance objects

前端 未结 2 467
执笔经年
执笔经年 2021-01-26 02:11

I created many instances of a PlotHandler class. An instance must keep it\'s variables private. But the way I managed them led to a hard to detect problem,

2条回答
  •  温柔的废话
    2021-01-26 02:48

    See this question, this one, and tons of other stuff you can find by googling "Python class variables shared", "Python FAQ class variables", etc.

    The short answer is: variables defined directly in the class body are class variables, not instance variables, and are thus shared among instances of the class. If you want instance variables you must assign them from within a method, where you have access to self.

提交回复
热议问题