__del__ 方法在 python 中被意外调用
我是 python 新手,一直在研究 Swaroop CH 的“A Byte of Python”中的示例。我看到 __del__
方法的一些行为令我困惑。
运行以下脚本(在 Python 2.6.2 中),
class Person4:
'''Represents a person'''
population = 0
def __init__(self, name):
'''Initialize the person's data'''
self.name = name
print 'Initializing %s'% self.name
#When the person is created they increase the population
Person4.population += 1
def __del__(self):
'''I am dying'''
print '%s says bye' % self.name
Person4.population -= 1
if Person4.population == 0:
print 'I am the last one'
else:
print 'There are still %d left' % Person4.population
swaroop = Person4('Swaroop')
kaleem = Person4('Kalem')
基本上,如果我使用 Python 控制台(或 Spyder 交互式控制台)
我会看到以下内容: <块引用> <块引用>execfile(u'C:\1_eric\Python\test1.py')
初始化 Swaroop
初始化卡勒姆execfile(u'C:\1_eric\Python\test1.py')
初始化 Swaroop
斯瓦鲁普说再见
我是最后一个
初始化 Kalem
卡勒姆说再见
我是最后一个
为什么在第二次运行时会在 __init__
之后立即调用 __del__
方法?
我猜测,由于使用了相同的实例名称(“swaroop”和“kaleem”),因此它正在释放原始实例并对其进行垃圾收集。但是,这似乎对当前的人口数量造成了严重破坏。
这是怎么回事?
避免这种混乱的好方法是什么?
避免使用__del__
? 在重用之前检查现有实例名称? ...
谢谢, 埃里克
I am new to python and have been working through the examples in Swaroop CH's "A Byte of Python". I am seeing some behavior with the __del__
method that is puzzling me.
Basically, if I run the following script (in Python 2.6.2)
class Person4:
'''Represents a person'''
population = 0
def __init__(self, name):
'''Initialize the person's data'''
self.name = name
print 'Initializing %s'% self.name
#When the person is created they increase the population
Person4.population += 1
def __del__(self):
'''I am dying'''
print '%s says bye' % self.name
Person4.population -= 1
if Person4.population == 0:
print 'I am the last one'
else:
print 'There are still %d left' % Person4.population
swaroop = Person4('Swaroop')
kaleem = Person4('Kalem')
using the Python console (or the Spyder interactive console) I see the following:
execfile(u'C:\1_eric\Python\test1.py')
Initializing Swaroop
Initializing Kalemexecfile(u'C:\1_eric\Python\test1.py')
Initializing Swaroop
Swaroop says bye
I am the last one
Initializing Kalem
Kalem says bye
I am the last one
Why is the __del__
method being called immediately after the __init__
on the second run?
I am guessing that since the same instance names ('swaroop' and 'kaleem') are being used that it is releasing the original instance and garbage collecting it. But, this seems to be playing havoc with the current population count.
What is going on here?
What is a good way to avoid this sort of confusion?
Avoid the use of __del__
?
Check for existing instance names before reusing them?
...
Thanks,
Eric
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里发生了一些事情。当您的
Person4
类实例化时,它会将其population
类变量初始化为 0。在交互式控制台中,您似乎多次运行“test1.py”文件。第二次运行它时,会再次声明Person4
类,这使得它在技术上与第一次不同(即使它具有相同的名称)。这意味着它有自己独立的人口
计数。现在,
swaroop
和kaleem
是全局变量,在“test1.py”的两个实例之间共享。 Python 在内部使用引用计数来进行大部分自动垃圾回收,因此第一个Person4
类的原始实例直到第二次分配给swaroop
时才会被释放。分配给 swaroop 会减少第一个实例的引用计数,从而导致调用 __del__ ,因为引用计数现在为零。但因为您在__del__()
中通过名称引用Person4
,所以当前一个实例消失时,它会减少新实例em>Person4.population
计数,而不是旧的Person4
人口计数。希望这是有道理的。我明白为什么这可能会让学习 Python 的人感到困惑。在使用
execfile()
重新定义Person4
类的同时使用类变量会进一步造成混乱。无论如何,我已经编写了很多 Python 代码,并且我认为我不需要使用__del__
特殊方法。There are a couple of things going on here. When your
Person4
class is instantiated, it initialises itspopulation
class variable to 0. From your interactive console, you appear to be running your "test1.py" file multiple times. The second time you run it, thePerson4
class is declared again which makes it technically different from the first one (even though it has the same name). That means it has its own independentpopulation
count.Now,
swaroop
andkaleem
are global variables, shared between both your instances of "test1.py". Python internally uses reference counting for most of its automatic garbage collection, so the original instance of the firstPerson4
class is not released until the second assignment toswaroop
. Assigning toswaroop
decrements the reference count for the first instance, causing__del__
to be called because the reference count is now zero. But because you're referring toPerson4
by name inside__del__()
, when the previous instance disappears it decrements the newPerson4.population
count, instead of the oldPerson4
population count.Hopefully that made sense. I can see why this might be confusing to somebody learning Python. Your use of class variables at the same time as redefining the
Person4
class usingexecfile()
is further confusing matters. For what it's worth, I've written a lot of Python code and I don't think I've ever needed to use the__del__
special method.一般建议:不要在 Python 中使用 __ del __。它可以通过多种方式破坏垃圾收集,特别是。在对象之间循环引用的情况下。
在您的示例中,存在与 execfile() 的使用(这不是最佳实践)以及全局变量的重新定义相关的各种问题。顺便说一句,如果您确实需要创建一个伪析构函数(即每当对象被垃圾收集时调用的代码),请编写一个所谓的“终结器”函数(它不是一个正确的析构函数)并使用weakref调用它.ref 回调。当然,它不应该是一个实例方法,并且记住 lambda 实际上创建了一个闭包,因此请确保不要在回调中泄漏对 self 的任何引用!如果您需要来自被销毁实例的数据,请使用 func 默认参数方法,只需确保从不在 lambda 内引用“self”,否则它将无法工作。
输出(睡眠是为了帮助了解发生了什么):
General advice: don't use __ del __ in Python. It can break garbage collection in a number of ways, esp. in the case of circular references between objects.
In your example, there're various issues related to the usage of execfile() - which is not a best practice - and the redefinition of global variables. By the way, if you really need to create a pseudo-destructor (i.e. a code that is invoked whenever the object gets garbage collected), write a so-called "finalizer" function (it's not properly a destructor) and invoke it using weakref.ref callback. It should NOT be an instance method of course, and remember that lambda actually creates a closure, hence be sure not to leak any reference to self in the callback! If you need data from the destroyed instance, use the func default argument approach, just be sure never to reference 'self' inside the lambda, otherwise it won't work.
output (the sleep is there to help see what's happening):