创建实例失败时析构函数会被调用吗?

3

当我试着理解一些Python概念时,我遇到了以下问题:

class A:
    def __init__(self, x):
        self.x = x

    def __del__(self):
        print("del")

a1 = A()

输出:

$ python test.py
del
Traceback (most recent call last):
  File "testdest.py", line 9, in <module>
    a1 = A()
TypeError: __init__() takes exactly 2 arguments (1 given)

错误很明显(实例化时缺少参数),但我想知道为什么在有实例之前就调用了析构函数? 除非在尝试实例化时,Python 在调用构造函数之前创建了某种实例,这需要在最后清理吗?
由于self被传递给构造函数,我可以假设这个self是实例吗?如果是这样,那么当调用构造函数时,实例已经存在了,对吗?
这是否是垃圾回收器的行为,可能取决于当前的实现?

我可能错了,但我通常认为__del__类似于finally子句,即无论如何都会执行的东西。 - Kris
1
@Kris 有点像。但是不能保证当解释器退出时仍然存在的实例会调用__del__ - chepner
1个回答

5

来自Python文档:

Objects are never explicitly destroyed; however, when they become unreachable they may be garbage-collected. An implementation is allowed to postpone garbage collection or omit it altogether — it is a matter of implementation quality how garbage collection is implemented, as long as no objects are collected that are still reachable.

object.__init__(self[, ...])

Called after the instance has been created (by new()), but before it is returned to the caller. [...]

object.__del__(self)

Called when the instance is about to be destroyed. [...]

因此,当调用__init__时,对象实例已经存在,因为它是由__new__创建的。但对于Python来说,无法保证__del__会被调用。

以下仅适用于Python的参考实现CPython。

注意(对于object.__del__(self)

del x不直接调用x.__del__()——前者将x的引用计数减少1,只有当x的引用计数达到零时才会调用后者。[...]

在这里,__del__仅在实例的引用计数降至0时调用。这与垃圾收集无关。做一个小实验:

>>> class A:
...   def __del__(self): print "del"
... 
>>> a = A()
>>> a = None
del
>>> import gc
>>> gc.disable() 
>>> a = A()
>>> a = None
del

正如你所见,即使显式禁用GC,析构函数仍将被调用。

请注意,这也意味着如果您的对象层次结构中存在循环引用,您最终会得到一些对象,其中__del__永远不会被调用,因为Python GC不能处理引用循环。

>>> a1 = A()
>>> a2 = A()
>>> a1.x = a2
>>> a2.x = a1
>>> a1 = None
>>> a2 = None
>>> import gc
>>> gc.collect()
4
>>> gc.garbage
[<__main__.A instance at 0x7f2c66a1d7e8>, <__main__.A instance at 0x7f2c66a1d830>]

2
__del__方法会在实例的引用计数降为0时被调用,同时垃圾回收器也会对其进行处理。这个过程发生的时间取决于Python解释器所使用的算法,不同的实现方式也会导致差异。在极端情况下,直到整个程序退出之前,这个过程甚至可能都没有发生。” - Byte Commander
@Markus,我觉得你的答案非常有趣,但是我只需要关于你最后一句话的解释,因为我没有完全理解:“每当实例的引用计数降至0时。”我可能会说一些愚蠢的话,但这是否意味着“每当该实例不再分配给一个变量。”? - vmonteco
1
@vmonteco看看这个链接:https://docs.python.org/2/reference/datamodel.html#object.__del__。这个能解决您的问题吗? - Markus
@Markus 我切换到了Python3文档版本,我认为它可以,谢谢! :) - vmonteco
1
@vmonteco,我添加了一个小例子,用于说明__del__永远不会被调用的情况。 - Markus
@markus Python并不使用引用计数。CPython(Python的官方实现)使用引用计数。在CPython中,当一个对象的引用计数达到0时,del方法会被无条件调用。然而,垃圾收集器存在的目的是处理对象不可访问但具有正引用计数(循环引用)的情况。 - Dunes

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接