does python have useless destructors?

Isaac To kkto at csis.hku.hk
Sun Jun 13 11:15:29 EDT 2004


>>>>> "David" == David Turner <dkturner at telkomsa.net> writes:

    David> Isaac, I think you've missed the main thrust of my suggestion
    David> here.  I'm not talking about "locally scoped objects" such as C++
    David> has.  I'm also not talking about modifying the garbage collector
    David> (if any).  I'm talking about a mechanism that is *independent* of
    David> the garbage collector.  To briefly resummarize:

    David> Objects with a __del__ method shall be reference counted.  When
    David> the reference count reaches zero, the __del__ method shall be
    David> called, and any subobjects that have a __del__ method shall also
    David> be unreferenced.

I'll love to have an easy way to have some objects reference counted and
some not.  But it, quite simply put, doesn't work.  If you have an object
with a __del__ method, you want it to be reference counted.  But what will
happen if instance of another class holds a reference to it?  It gotta be
reference counted as well, otherwise that object will not be deallocated and
an extra reference to the instance of the object that desire reference count
will prevent it from being deallocated.  And what if instance of yet another
class holds a reference to it?  It, again, has to be reference counted also.
And don't forget that most of such type of objects are actually very generic
objects that are everywhere in the language, dictionaries about the
variables in particular.  And then, what if that object does not declare a
__del__?  Actually, at least all the references of that object must go away
whether or not a __del__ is declared, otherwise the scheme won't work
because other objects are still holding references to your object.  So
essentially, your suggestion would make all objects reference counted, which
is exactly what C-Python is doing, and is exactly what Jython is determined
*not* to do (because doing so will require an extra layer of abstraction
when on Java objects).

Regards,
Isaac.



More information about the Python-list mailing list