does python have useless destructors?

Manlio Perillo NOmanlio_perilloSPAM at libero.it
Thu Jun 17 03:53:41 EDT 2004


On Tue, 15 Jun 2004 11:26:33 -0700, Donn Cave <donn at u.washington.edu>
wrote:

>In article <m3fz8xozi1.fsf at pc150.maths.bris.ac.uk>,
> Michael Hudson <mwh at python.net> wrote:
>> Manlio Perillo <NOmanlio_perilloSPAM at libero.it> writes:
>...
>> > Since __del__ isn't really 'useful', *maybe* a better solution is to
>> > add another special method for classes, ad example a __finalize__
>> > method.
>> 
>> Yes!  I'm not sure __finalize__ is really the best name, but that's
>> for another day.
>
>Couldn't the name be __del__?  Given the opportunity to have
>both, and the assurance that __finalize__ will be called and
>__del__ might not, what functionality would you leave in __del__?
>

Since objects construction is a two phase operation, maybe also the
destruction can be realized in two phases:
__new__ -> __init__ -> ... -> __deinit__ -> __del__


>
>I guess there isn't much point in proposing improvements for
>finalization and __del__, as long as there's a commitment to
>support Python on a platform without support for finalization
>like Java.  But never one to be deterred by pointlessness,
>suppose __finalize__ were a flag, instead of a method.  It
>has two functions:  1.  declare that the object's __del__
>method should be called when it's logically unreferenced -
>either no references, or only referenced as part of a cycle
>or traceback.  2.  Serve as the extra reference count that's
>needed for this, so __del__ will only be called once regardless
>of further reference decrements, cycle analysis etc.  Default
>would be no __finalize__, but it should probably be added to
>some class/types, e.g., fileobject.
>


Another solution would be to have an attribute for every classes:
del_func.
del_func by default is None and it is called during stack unwinding.
(Even if there are references to the object)

Programmer that want to use the C++ RAII pattern can do:
obj.del_func = obj.__del__



Regards   Manlio Perillo




More information about the Python-list mailing list