python-3.x - 创建实例失败时调用的析构函数?

标签 python-3.x garbage-collection destructor instantiation

在尝试理解一些 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>]

关于python-3.x - 创建实例失败时调用的析构函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36983085/

相关文章:

python - for 循环如何评估其参数

java - 高 cpu/gc Activity 随后崩溃

python - 无法从 python 2.7 中的 atexit 注销函数

c++ - std::pair 和类析构函数

c++ - 用户定义的 Vector 类的自动类型转换?

Python 3,如何将字符串转换为 "iso-8859-1"以在html中使用

python - (任何)python 模块的类型提示是什么?

python-3.x - Falcon 和 falcon-multipart + POST 请求上传文件的实现

c++ - 虚拟析构函数导致访问冲突

java - java无限循环什么时候停止?