我不了解python __del__的行为
有人可以解释为什么以下代码会如此行为:
import types
class Dummy():
def __init__(self, name):
self.name = name
def __del__(self):
print "delete",self.name
d1 = Dummy("d1")
del d1
d1 = None
print "after d1"
d2 = Dummy("d2")
def func(self):
print "func called"
d2.func = types.MethodType(func, d2)
d2.func()
del d2
d2 = None
print "after d2"
d3 = Dummy("d3")
def func(self):
print "func called"
d3.func = types.MethodType(func, d3)
d3.func()
d3.func = None
del d3
d3 = None
print "after d3"
输出(请注意,永远不会调用d2的析构函数)是(python 2.7)
delete d1
after d1
func called
after d2
func called
delete d3
after d3
有没有一种方法可以“修复”代码,以便在不删除添加的方法的情况下调用析构函数?我的意思是,放置d2.func = None的最佳位置将在析构函数中!
谢谢
[edit]基于前几个答案,我想澄清一下,我并不是在问using的优点(或不足之处)__del__
。我试图创建最短的函数来证明我认为是非直觉的行为。我假设已经创建了一个循环引用,但是我不确定为什么。如果可能的话,我想知道如何避免使用循环引用。
-
我提供自己的答案是因为,尽管我喜欢避免使用的建议
__del__
,但我的问题是如何使它在提供的代码示例中正常工作。简短版本: 以下代码用于
weakref
避免循环引用。我以为在发布问题之前就已经尝试过了,但是我想我一定做错了什么。import types, weakref class Dummy(): def __init__(self, name): self.name = name def __del__(self): print "delete",self.name d2 = Dummy("d2") def func(self): print "func called" d2.func = types.MethodType(func, weakref.ref(d2)) #This works #d2.func = func.__get__(weakref.ref(d2), Dummy) #This works too d2.func() del d2 d2 = None print "after d2"
较长的版本 :发布问题时,我确实搜索了类似的问题。我知道您可以
with
改用,并且普遍的看法__del__
是 BAD 。使用
with
是有意义的,但仅在某些情况下才有意义。打开,读取和关闭文件with
是一个很好的解决方案的好例子。您已经找到了需要该对象的特定代码块,并且想要清理该对象和该块的末尾。似乎经常使用数据库连接作为一个使用效果不佳的示例
with
,因为您通常需要离开创建连接的代码部分,并在更多的事件驱动(而不是顺序)的时间范围内关闭连接。如果
with
不是正确的解决方案,我会看到两种选择:当我尝试提供简化的代码时,我的真正问题更多是由事件驱动的,因此
with
不是一个合适的解决方案(with
适用于简化的代码)。我也想避免使用atexit
,因为我的程序可以长时间运行,并且希望能够尽快执行清理。因此,在这种特定情况下,我发现它是使用
weakref
并防止循环引用__del__
无法正常工作的最佳解决方案。这可能是该规则的例外,但是在某些情况下,使用
weakref
和__del__
是正确的实现,恕我直言。