If you define a class instance in a function, when the function exits, the instance will be automatically destroyed due to out of scope. This can be simply verified by a small program:
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()
The output is:
this is <__main__.B object at 0x01BC6150>
deleting B 29122896
deleting A 29122896
deleting A 29122960
But I'm encountering a weird problem. When I inherit a class from novaclient, the instance will never be automatically destroyed.
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()
Output is:
2013-05-24 23:08:03 32240 INFO vivi.vivimain.ViviNovaClient [-] creating <ViviNovaClient> 26684304
In this test, 'client' object is not destroyed. So I wonder what will prevent the 'client' object from auto destroying?