does python have useless destructors?

Roy Smith roy at panix.com
Fri Jun 11 14:06:04 CEST 2004


dkturner at telkomsa.net (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?



More information about the Python-list mailing list