简体   繁体   English

Python:什么可以防止对象破坏?

[英]python: what prevents from object destroy?

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. 当我从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()

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? 因此,我想知道什么会阻止“客户端”对象自动销毁?

Because a lot of other objects are storing a reference to the client too, forming many reference cycles . 由于许多其他对象也存储着对客户端的引用,因此形成了许多引用周期

For example, from the source code : 例如,从源代码

class Client(object):
    def __init__(self, username, **etc):
        password = api_key
        self.project_id = project_id
        self.flavors = flavors.FlavorManager(self)
        # etc.

We see that the Client will save a FlavorManager, and the initializer of FlavorManager (which is a Manager ) will save a reference to the input Client as well: 我们看到客户端将保存FlavorManager,而FlavorManager的初始化程序(即Manager )也将保存对输入Client的引用:

class Manager(utils.HookableMixin):
    def __init__(self, api):
        self.api = api

Until Python starts the cycle collector, these objects will not be deleted immediately. 在Python启动循环收集器之前,不会立即删除这些对象。 Note that adding the __del__ methods prevents the cycle collector from running . 请注意,添加__del__方法会阻止循环收集器运行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM