Serhiy Storchaka added the comment: > finalize does not "compete" with contextlib.ExitStack, atexit and > weakref.WeakKeyDictionary. It only competes with __del__ and weakref > callbacks.
What kind of problems you solve with __del__ and weakref callbacks? For most of them contextlib.ExitStack and atexit are safer and better fit. > Points 1 and 2 in my first message are the main points. Also, read the > warning at For point 1: global weakref.WeakKeyDictionary is good store for weak refs with callbacks. global weakdict weakdict[kenny] = weakref.ref(kenny, lambda _: print("you killed kenny!")) If you do not want to work at a low level, in most cases fit the above- mentioned high-level tools. For point 2 Antoine has noted that it is a known issue and there is a solution (issue812369). > http://docs.python.org/py3k/reference/datamodel.html#object.__del__ > > which also applies to weakref callbacks. Is this your point 2? > Other problems with __del__: > > * Ref cycles which contain an object with a __del__ method are immortal It is use case for weakrefs which break reference loops. > * __del__ methods can "ressurect" the object. What you are meaning? Relying on GC is dangerous thing for resource releasing. And it even more dangerous for alternative implementations without reference counting. That is why in recent times in Python there has been a tendency to RAII strategy (context managers). Can you give examples, where the use of finalize necessary or more convenient the other ways? ---------- _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue15528> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com