如果在函数中定义类实例,则当函数退出时,实例将因范围超出而自动销毁。这可以通过一个小程序简单地验证:
class A(object):
def __del__(self):
print 'deleting A ', id(self)
class B(A):
def __init__(self):
self.a = A()
def __del__(self):
print 'deleting B ', id(self)
super(B, self).__del__()
def test():
b = B()
print 'this is ', b
test()
输出结果为:
this is <__main__.B object at 0x01BC6150>
deleting B 29122896
deleting A 29122896
deleting A 29122960
但我遇到了一个奇怪的问题。当我从novaclient继承一个类时,实例永远不会被自动销毁。
from novaclient.v1_1.client import Client as NovaClient
class ViviNovaClient(NovaClient):
def __init__(self, auth_token, url, tenant_id):
super(ViviNovaClient, self).__init__(None, None, tenant_id, auth_url = 'http')
self.client.management_url = url
self.client.auth_token = auth_token
self.client.used_keyring = True;
LOG.info('creating <ViviNovaClient> %d' % id(self))
def __del__(self):
LOG.info('deleting <ViviNovaClient> %d' % id(self))
if __name__ == '__main__':
def test():
client = ViviNovaClient('53ef4c407fed45de915681a2d6aef1ee',
'http://135.251.237.130:8774/v2/082d8fd857f44031858827d149065d9f',
'082d8fd857f44031858827d149065d9f')
test()
输出是:
2013-05-24 23:08:03 32240 INFO vivi.vivimain.ViviNovaClient [-] creating <ViviNovaClient> 26684304
在此测试中,“客户端”对象不会被破坏。所以我想知道什么会阻止'客户'对象自动销毁?
答案 0 :(得分:5)
因为很多其他对象也存储了对客户端的引用,形成了很多reference cycles。
例如,来自source code:
class Client(object):
def __init__(self, username, **etc):
password = api_key
self.project_id = project_id
self.flavors = flavors.FlavorManager(self)
# etc.
我们看到客户端将保存FlavorManager,而FlavorManager的初始化程序(Manager)也将保存对输入客户端的引用:
class Manager(utils.HookableMixin):
def __init__(self, api):
self.api = api
在Python启动循环收集器之前,不会立即删除这些对象。请注意,添加__del__
方法prevents the cycle collector from running。