Destructors and exceptions
Dominic
nomail at nospam.no
Tue Jun 8 04:38:45 EDT 2004
David Turner wrote:
> 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
Well, there had been some discussion before. You can
wrap the open/close actions in a closure and write
something like with_file_do(myFunction).
Ciao,
Dominic
> 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