对于在类中的变量访问,具体细节如下:
class Base(object):
count = 0
def __init__(self):
self.count += 1
b1 = Base()
print(b1.count) # 对象 b1 的变量
print(Base.count) # 类 Base 本身的变量
Base.count = Base.count + 1 # 修改类本身的变量
print(Base.count)
b2 = Base()
print(b2.count) # 修改过的类 的 实体化 b2 的变量
b1.count = b1.count + 3
print(b1.count)
print(Base.count)
print(b2.count)
输出:
1
0
1
2
4
1
2