Python Class Members Initialization

前端 未结 5 1758
后悔当初
后悔当初 2020-11-29 03:37

I have just recently battled a bug in Python. It was one of those silly newbie bugs, but it got me thinking about the mechanisms of Python (I\'m a long time C++ programmer,

5条回答
  •  -上瘾入骨i
    2020-11-29 04:33

    @Matthew : Please review the difference between a class member and an object member in Object Oriented Programming. This problem happens because of the declaration of the original dict makes it a class member, and not an object member (as was the original poster's intent.) Consequently, it exists once for (is shared accross) all instances of the class (ie once for the class itself, as a member of the class object itself) so the behaviour is perfectly correct.

提交回复
热议问题