does python have useless destructors?

David Turner dkturner at
Fri Jun 11 22:38:24 CEST 2004

Roy Smith <roy at> wrote in message news:<roy-235923.08060411062004 at>...
> dkturner at (David Turner) wrote:
> > 1. Objects of classes that declare a __del__ method shall be referred
> > to as "deterministic" objects.  Such objects shall have a reference
> > count associated with.  The reference count shall be incremented
> > atomically each time an additional reference to the object is created.
> >  The reference count shall be decremented each time a name referring
> > to the object is deleted explicitly.  Except in the situation
> > described in (2) below, the reference count shall be decremented each
> > time a name referring to the object becomes inaccessible due to the
> > set of locals to which the name belongs becoming inaccessible.
> > [...]
> > 3. When the reference count of a deterministic object reaches zero,
> > the __del__ method of the object shall be called.
> What if you do this...
> class Top:
>    def __init__ (self):
>       self.bottom = Bottom ()
> class Bottom:
>    def __del__ (self):
>       do something really important
> top = Top ()
> del top
> The problem here is that while Bottom.__del__ will get called as soon at 
> top releases it's reference, there's nothing to guarantee that the 
> reference will disappear until top is garbage collected.  You could fix 
> that by writing Top.__del__, but that assumes Top is a class you have 
> control of (it might be something out of a library).
> Or am I not understanding the situation right?

This particular example you cite is not actually problematic.  The
point is that the resources are cleaned up when Top is cleaned up,
which is what we want.  How and when Top is cleaned is neither here
nor there.

However, you have touched on a deep issue.  When an object is cleaned
up, whether using an explicit __del__ or implicitly, any deterministic
objects it contains will of course have to be cleaned up (in the
reverse order of allocation, again).  With an explicit __del__, this
is probably okay.  But when a deterministic object is in the scope of
a non-deterministic object, there could be an implementation problem. 
Perhaps someone with more insight into Jython than I could shed some
light on whether or not this would be an issue?

David Turner

More information about the Python-list mailing list