is there anybody using __del__ correctly??

Steven Bethard steven.bethard at gmail.com
Fri Aug 10 13:09:59 EDT 2007


Raymond Hettinger wrote:
> [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.

There were also a few recipes posted during this discussion that wrap 
weakrefs up a bit nicer so it's easier to use them in place of __del__:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/519635
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/519610
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/519621

Since they're using weakrefs, you shouldn't have to worry about some of 
the weirdness of __del__.

STeVe



More information about the Python-list mailing list