Destructors and exceptions

David Turner dkturner at telkomsa.net
Tue Jun 8 02:01:43 EDT 2004


Hi

> If you want to force the issue, give your class a close method, as with
> file objects, and call it explicitly.  Then code in try:finally:, which is
> designed for this purpose.
> try:
>    process
> finally:
>    cleanup
> 

That's a great shame.  "Finally" is of course susceptible to the
"forgetful programmer" syndrom.  It also pollutes the code with what I
consider to be implementation details -- for example, the fact that a
file must be closed is a characteristic of the file object, and only
accidentally relevant to the _use_ of files.  Python would be a much
stronger language if it could guarantee deterministic destruction of
at least those objects which have a __del__ method.  Can anyone point
out any technical reasons why such a feature should not be included in
the language?

At any rate, I wonder if there is an another solution to the problem
of resource management.  Perhaps something along the lines of Ruby
closures?  Any suggestions?  There has to be something more elegant
(and safer!) than "finally".

Regards
David Turner



More information about the Python-list mailing list