does python have useless destructors?

Robert Brewer fumanchu at amor.org
Thu Jun 10 12:35:27 EDT 2004


Duncan Booth wrote:
> "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.

Yes, I think that was where I diverged: I'd rather not mask the error in
dispose.

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

Actually, I meant argnames, and just didn't call it correctly. :)

> >     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.

Hmmmm... I'm not sure I agree. Again, we're talking about which error to
raise from a potential host of them. I just find the linear approach
more straightforward--if it errors, raise it. Otherwise testing becomes
a royal pain. But then, I'm not coding nuclear reactors.

Given that some apps will want guaranteed disposal calls and some won't,
I'd prefer to push the desired exception handling into each __dispose__
method. Hmmm. No free brain cells at the moment. :(

Thanks for the comments!


Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org




More information about the Python-list mailing list