简体   繁体   English

Python:了解对象__del__方法

[英]Python: Understanding object __del__ method

Can anyone describe me why this code will print '2 1 0 done' instead of expected output '0 1 2 done'? 谁能描述我为什么此代码将打印“ 2 1 0完成”而不是预期的输出“ 0 1 2完成”? As i can understand, we have some anonymous variables creating during list comprehensions, and they are garbage-collected, using filo principle, on list comprehension uncoverage end. 据我了解,我们在列表理解期间创建了一些匿名变量,并且使用filo原则在列表理解发现端对它们进行了垃圾收集。 But, they still are referenced in list aa, aren't they? 但是,它们仍然在列表aa中被引用,不是吗? Why the second 'del a' is not calling del magic method in that case? 在这种情况下,为什么第二个“ del a”没有调用del magic方法?

class A:
    def __init__(self, name):
        self.name = name
    def __del__(self):
        print self.name,

aa = [A(str(i)) for i in range(3)]
for a in aa:
    del a

print 'done'

Also, advanced questions. 还有,高级问题。 Please look at http://codepad.org/niaUzGEy Why there are 5 copies, 3 copies? 请查看http://codepad.org/niaUzGEy为什么会有5份,3份? Musn't this be 1 copy? 这不是一本吗? Why 5 or 3? 为什么是5或3? Dont know, thats why asking it ;) Thanks for your time! 不知道,那就是为什么要问它;)谢谢您的时间!

You are confusing the del statement and the __del__ method. 您会混淆del语句和__del__方法。

del a simply unbinds the name a from whatever object it referenced. del a只是将名称a从其引用的任何对象中解除绑定。 The list referenced by aa is unchanged so the objects all continue to exist. aa引用的列表未更改,因此所有对象继续存在。

The __del__ method is only called after the last reference to an object has been destroyed. 仅在对对象的最后一个引用被破坏之后才调用__del__方法。 That could be after a call to __del__ but usually isn't. 可能是在调用__del__但通常不是。

You rarely need to use del . 您很少需要使用del It would be much more common just to rebind aa and then all the objects it contains will be released, and if not otherwise referenced their __del__ methods will be called automatically. 重新绑定aa ,然后释放其中包含的所有对象,这将更为常见,如果没有另外引用,则将自动调用其__del__方法。

Also, you rarely need to use __del__ . 另外,您很少需要使用__del__ For most purposes Python's management of objects will handle cleanup automatically. 对于大多数目的,Python的对象管理将自动处理清理。 Adding a __del__ method to a class is generally a bad idea as it can interfere with the garbage collector, so rather paradoxically __del__ makes it more likely that your program will leak memory. 在类中添加__del__方法通常是一个坏主意,因为它可能会干扰垃圾回收器,因此反而__del__使您的程序更有可能泄漏内存。 Also Python won't guarantee whether __del__ is actually called on program exit, and if it does you may find global variables you cant to use no longer exist, nor will it guarantee to only call it once (though you have to jump through hoops to make it call it more than once). Python也无法保证__del__是否在程序退出时真正被调用,如果这样做,您可能会发现无法使用的全局变量不复存在,也不能保证仅调用一次(尽管您必须跳过循环才能使其多次调用)。

In short, avoid using __del__ if you possibly can. 简而言之,请尽可能避免使用__del__

It prints done 2 1 0 (CPython). 它打印done 2 1 0 (CPython)。

You don't delete list elements in a for loop. 您不会在for循环中删除列表元素。 They are deleted on exit. 它们在退出时被删除。 As far as I know call order of __del__ is implementation-specific, so it can be different in another implementations(IronPython, Jython etc.) 据我所知__del__调用顺序是特定于实现的,因此在其他实现(IronPython,Jython等)中可以有所不同。

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

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