创建实例失败时调用的析构函数?

destructor called when failing to create the instance?

在试图理解一些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 是实例吗?原来如此,那么调用构造函数时实例已经存在了,是吗?

这是垃圾收集器的一种行为,可能取决于当前的实现吗?

来自 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__ 会被调用。

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

Note (for object.__del__(self))

del x doesn’t directly call x.__del__() — the former decrements the reference count for x by one, and the latter is only called when x‘s reference count reaches zero. [...]

这里 __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>]