does python have useless destructors?

Duncan Booth me at privacy.net
Thu Jun 10 12:29:36 EDT 2004


"Robert Brewer" <fumanchu at amor.org> wrote in 
news:mailman.819.1086881457.6949.python-list at python.org:

> AFAICT, you don't need to reraise the exception. Example:
> 
>>>> try:
> ...      f
> ... finally:
> ...      print 'finally'
> ...      
> finally
> Traceback (most recent call last):
>   File "<interactive input>", line 2, in ?
> NameError: name 'f' is not defined
> 
> 
> But I could be wrong--perhaps you've got a requirement I can't see on
> first glance.

I'll try to explain my intention a bit more clearly. As you note, if the 
try block throws then you don't need to re-raise the exception. What I was 
trying to do was to handle the case where the dispose code throws an 
exception. If that happens when we already have an exception, then I want 
to mask the second exception and continue with the first, but if it happens 
when there is no exception I want the exception to be thrown.

I am quite willing to be persuaded though that the correct action would be 
that an exception thrown in the dispose be allowed to mask the inner 
exception.



> 
> def dispose(localdict, argnames):
you meant *argnames

>     for name in argnames:
>         obj = localdict.get(name)
>         if obj:
>             try:
>                 dispfunc = obj.__dispose__
>             except AttributeError:
>                 pass
>             else:
>                 dispfunc()
> 

Close, but not quite there. I think you need to ensure that all of the 
__dispose__ methods *must* be called, even if an earlier one fails.




More information about the Python-list mailing list