Reputation: 52263
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 execute? Or can each implementation do what it likes, potentially delaying the call to __del__
arbitrarily long?
Please ignore the situation when the program is about to exit (I assume it means the last statement in the given block has finished, and the stack is empty). I understand that in such cases, there's no promises about __del__
; it may not even be called at all.
Also, I'm aware that reference count may be non-zero due to cycles, etc. I am not concerned about that here (I'm asking a separate question about it).
Upvotes: 4
Views: 1340
Reputation: 156158
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.
Upvotes: 4
Reputation: 133425
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.)
Upvotes: 11
Reputation: 90762
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.
Upvotes: 3