does python have useless destructors?

David Turner dkturner at telkomsa.net
Sun Jun 13 08:00:55 EDT 2004


Isaac To <kkto at csis.hku.hk> wrote in message news:<7iy8msdf8u.fsf at enark.csis.hku.hk>...
> >>>>> "David" == David Turner <dkturner at telkomsa.net> writes:
> 
>     David> Also, I'd like to point out that destructor semantics and GC are
>     David> not necessarily related.  There's no rule that says the
>     David> destructor has to be called at the same time as the memory is
>     David> freed.  In fact, there are several good reasons for separating
>     David> the events.
> 
> Unluckily, currently there is no concept of "local scoped objects" in
> Python.  What it means is that a variable can be local, but an object
> cannot.  An object in Python is always heap allocated, so there is no way to
> know that whether an object has a reference staying outside the current
> scope when the current scope exits---short of dictating how the garbage
> collector works (i.e., it must count reference).  I think the primary
> developer in Python has already completely rejected the very idea, probably
> because it is impractical to implement in Jython.
> 

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

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

The point at which the memory allocated to the object is freed is
largely irrelevant.  The point is that there's a predictable time at
which __del__ is called.  This is what enables the RAII idiom.

Now, this could be tricky to implement because we are now separating
the concepts of "destruction" and "finalization".  But it's certainly
not impossible, and it would add a powerful new concept to the
language.  So I don't think the idea should be rejected out of hand.

Is this clearer?

Regards
David Turner



More information about the Python-list mailing list