Python Static Class attributes
This question already has an answer here:
为了让你的代码像你想要的那样工作,使用myClass.__a
来访问变量,而不是self.__a
。
def increase_A(self):
myClass.__a += 1
return
def get_A(self):
return myClass.__a
They start off as the same variable. However, when you do
self.__a += 1
this rebinds the object's __a
to a new object whose value is 1
.
It does not change any other object's __a
so the code will print out 0
.
上一篇: Python类变量的范围不是按照文档
下一篇: Python静态类属性