On 5/01/20 11:41 am, Antoine Pitrou wrote:

+1 for pointing to `weakref.finalize` as a better
alternative for things that may survive until interpreter shutdown.

I'm not sure that weakref.finalize is much better. It can still be
called at an arbitrary time from an arbitrary thread with the rest
of the system in an unknown state, so it needs to be written very
defensively to cope with that.

The main thing I can see weakref.finalize having over __del__ is
that it doesn't give you access to the destroyed object, so you
can't accidentally rely on some part of it that's no longer valid.
But that's only a subset of the ways it can get you into trouble.

I don't think a blanket warning against using __del__ would be
very helpful, because many of its pitfalls also apply to the
suggested alternatives. This is an area where there's no
substitute for an in-depth understanding of the issues.

--
Greg
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/DHH6QWTHSD4TCQ2V2BEMHYG6K36KRSHY/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to