gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 02:23:38 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com