is there anybody using __del__ correctly??

Raymond Hettinger python at rcn.com
Fri Aug 10 12:21:48 EDT 2007


[Michele Simionato]
>  Here and there I hear rumors about deprecating __del__ and
> nothing
> happens, are there any news about that? Expecially concerning Py3k?

I was writing a Py3K PEP advocating the elimination of __del__
because:

* 'with closing()' and try/finally are the preferred ways of calling
close()

* it is easy to accidently keep or introduce a reference that blocks
the __del__ logic

* under-the-hood, the implementation of __del__ clashes badly with GC
logic and is a maintenance nightmare

* the body of a __del__ method may unintentionally resurrect an object
that was in the process of being deleted

For the PEP to have a chance, I neede to make build-outs to the
weakref module so that existing use cases for __del__ can be easily
migrated.  That hasn't been done yet, so the campaign to eliminate
__del__ is stalled.

> I should mention that if you search comp.lang.python
> for __del__ you will find hundreds of people who were
> bitten by __del__, so I usually give advices such as
> "you should never __del__ in your code"

Good advice. Explicit finalization is almost always preferable.



Raymond Hettinger




More information about the Python-list mailing list