假设我有以下课程
class Daddy:
children=[]
def addChild(self,aChild):
self.children.append(aChild)
class Child:
name = ''
def __init__(self, aName):
self.name = aName
aChild = Child('Peter')
aDaddy = Daddy()
aDaddy.addChild(aChild)
print aDaddy.children[0].name
del(aDaddy)
anotherDaddy = Daddy()
print anotherDaddy.children[0].name
Daddy() 保留对对象 aDaddy 的引用,我得到以下输出:
Peter
Peter