Is there any obstacle that prevents weakref
from doing everything that __del__
does but with much stronger guarantees (e.g., finalize
guarantees that the call will be made before the interpreter exits, and the order of calls is well-defined, etc.)?
It seems that in the distant past it was thought that weakref
would eventually lead to the removal of __del__
from the language.
What prevented this from happening?
There seems to be few use cases for __del__
, and all the ones I'm aware of seem to work at least as well (and usually much better) with weakref
callbacks or weakref.finalize
.
Update:
With PEP 442 dramatically improving the behavior of __del__
, and the concerns with weakref
mentioned by @gz and @user2357112, I'm wondering if the language is generally moving towards making __del__
more reliable, or towards using weakref
instead of __del__
, or both.
Python contains the weakref module that creates a weak reference to an object. If there are no strong references to an object, the garbage collector is free to use the memory for other purposes. Weak references are used to implement caches and mappings that contain massive data.
weakref. proxy(object[, callback]) – This returns a proxy to object which uses a weak reference. weakref. getweakrefcount(object) – Return the number of weak references and proxies which refer to object.
WeakValueDictionary ([dict]) Mapping class that references values weakly. Entries in the dictionary will be discarded when no strong reference to the value exists any more.
There's a somewhat pragmatic reason __del__
is still around. Several signficant weakref
improvements, including finalize
, were new in Python 3.4. So, replacing __del__
with better weakrefs missed the window for language breaking changes with py3k.
I think most uses can be replaced by the base weakref functionality, but I'm struck by this observation from Richard Oudkerk in issue 15528 where proposed and implemented finalize
:
[Weakref callbacks] are low level, and working out how to use them correctly requires a bit of head scratching. One must find somewhere to store the weakref till after the referent is dead, and without accidentally keeping the referent alive. Then one must ensure that the callback frees the weakref (without leaving any remnant ref-cycles).
When it is an option, using a
__del__
method is far less hassle.
Anyway, perhaps the question should be brought up again when Python 4 is being considered? ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With