how soon is `__del__` called after reference count drops to zero?

后端 未结 3 1723
不思量自难忘°
不思量自难忘° 2021-01-20 00:00

How soon after the reference count reaches zero is __del__ method called? Does the language promise that it\'s done right away, before any other use code can ex

相关标签:
3条回答
  • 2021-01-20 00:22

    Please read the CPython object.__del__ docs. This says most of what needs to be said, but is implementation specific; most other Python implementations don't use refcounting and so you shouldn't depend on it being called at such and such a time (or even being called at all) for functionality. It gets called at the time of destruction—which will vary between implementations.

    0 讨论(0)
  • About the only reason you should ever use __del__ is to help the garbage collector collect more garbage. For instance, if you are implementing something like the ctypes module that attaches shared libraries to the running process, it makes sense to unload those libraries when the last reference to it is collected, to allow its address space to be used for something else.

    For managing other kinds of resources, you almost certainly don't want to have anything to do with the garbage collector. The correct tool for this is context managers. Unlike languages such as C++ or Ada where variable scope is used for RAII, python uses the with statement, along with an object that has an __enter__ and __exit__ method. Many built in python types use this exact mechanism to make sure finalization steps actually occur:

    >>> x = file("foo", 'w')
    >>> x.closed
    False
    >>> with x:
    ...     x.write("bar")
    ... 
    >>> x.closed
    True
    

    This is also valuable from the point of view of the zen of python:

    Explicit is better than implicit.

    because it is clear that cleanup is occuring, it is explicitly written that way. This is much better than the case of cleanup happening "magically" when some hidden variable (the reference count, if one exists, and it doesn't in PyPy or IronPython or Jython) reaches some particular value, maybe.

    0 讨论(0)
  • 2021-01-20 00:38

    Python doesn't make any guarantees about when __del__ is called, or whether it is called at all. As it is, __del__ methods are unlikely to be called if the object is part of a reference cycle, because even if the cycle as a whole is cleaned up, Python has no way to decide where to break the cycle and in what order the __del__ methods (if any) should be called. Because of __del__'s rather quirky semantics (in order to call __del__ the refcount of the object is temporarily increased, and the __del__ method can prevent destruction of the object by storing the reference somewhere else) what happens in other implementations is a bit of a crapshoot. (I don't remember the exact details in current Jython, but it has changed a few times in the past.)

    That said, in CPython, if __del__ is called, it's called as soon as the reference count drops to zero (since refcounting is the only way __del__ methods are called, and the only chance CPython has of calling __del__ is when the actual refcount is changed.)

    0 讨论(0)
提交回复
热议问题