如果在函数中定义了一个类实例,当函数退出时,该实例会因为超出范围而被自动销毁。这可以通过一个小程序简单验证:
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
在这个测试中,'client' 对象没有被破坏。所以我想知道什么会阻止“客户端”对象自动销毁?