我對Python(2.6.5)的這種行爲感到困惑,有人可以解釋爲什麼會發生這種情況嗎?由另一個實例繼承對父類的更改的Python對象實例
class A():
mylist=[]
class B(A):
j=0
def addToList(self):
self.mylist.append(1)
b1 = B()
print len(b1.mylist) # prints 0 , as A.mylist is empty
b1.addToList()
print len(b1.mylist) # prints 1 , as we have added to A.mylist via addToList()
b2 = B()
print len(b2.mylist) # prints 1 !!! Why ?????
謝謝!那很快! :-) – PyNoob123 2010-07-17 19:33:37
只是很好的時機:) – Skilldrick 2010-07-17 19:35:35