class A(object):
aalist = []
ai = 0
def __init__(self):
self.ablist = list()
def add(self):
self.aalist.append(["a","a"])
self.ablist.append(["b","b"])
self.ai = self.ai + 1
class B(A):
def __init__(self):
A.__init__(self)
self.bblist = list()
def badd(self):
self.bblist.append(['c','c'])
for i in range(1,4):
c = B()
c.add()
c.badd()
print c.aalist,c.ablist,c.bblist,c.ai
运行这些代码,结果提示:
[['a', 'a']] [['b', 'b']] [['c', 'c']] 1
[['a', 'a'], ['a', 'a']] [['b', 'b']] [['c', 'c']] 1
[['a', 'a'], ['a', 'a'], ['a', 'a']] [['b', 'b']] [['c', 'c']] 1
我不知道为什么 list 在循环中更新,而 int 似乎保持静态