__del__ doesn't work

Duncan Booth me at privacy.net
Mon Feb 16 11:38:37 CET 2004

PeterAbel at gmx.net (Peter Abel) wrote in 
news:21064255.0402151141.51a38fed at posting.google.com:

> But the statements
> import gc
> and
> gc.collect()
> should assure that when the reference counter goes to zero
> some time the __del__ method is executed.

If the class is involved in a cycle, then the __del__ method is never 

The garbage collector will simply refuse to collect any objects which have 
__del__ methods. The reason for this is quite simple: if A and B refer to 
each other, and both have __del__ methods, which class should be released 
first? Python refuses to guess, so neither is released.

The garbage collector can trigger a __del__ method when some objects are in 
a cycle and none of them have __del__ methods; then the cycle may be 
released and if this causes some other objects to lose their last reference 
then they will be released and associated __del__ methods called.

More information about the Python-list mailing list