does python have useless destructors?

Michael Hudson mwh at python.net
Tue Jun 15 07:02:30 EDT 2004


Manlio Perillo <NOmanlio_perilloSPAM at libero.it> writes:

> On Wed, 9 Jun 2004 20:40:11 +0000 (UTC), "Michael P. Soulier"
> <msoulier at digitaltorque.ca._nospam> wrote:
> 
> > [...]
> 
> Hi.
> 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.

> Such method, if present, *will* be called during stack unwinding.
> So, in __finalize__ one can release 'external' resources.
> 
> class file:
> 	__init__(self, ...): ...
> 
> 	__finalize__(self): self.close()
> 
> 	...
> 
> 
> Another useful addition could be to add a 'block' statement:
> 
> a_file = '...'
> 
> block:
> 	f = open(a_file)
> 	f.write(...)
> 
> 
> As an example, block can be supposed to be equivalent to:
> 
> block:
> 	statement
> 
> =>
> 
> def __block()
> 	statement
> __block()

I would urge everyone participating in this thread to read PEP 310,
the email conversation linked therein and (optional) *understand* it.

Hmm, good sigmonster :-)

Cheers,
mwh

-- 
  For every complex problem, there is a solution that is simple,
  neat, and wrong.                                    -- H. L. Mencken



More information about the Python-list mailing list