[Python-Dev] Pre-PEP: Unifying try-except and try-finally

Fredrik Lundh fredrik at pythonware.com
Fri May 6 18:36:57 CEST 2005


François Pinard wrote:

> > >     f = None
> > >     try:
> > >         f = action1(...)
> > >     ...
> > >     finally:
> > >         if f is not None:
> > >             action2(f)

> > f = action1()
> > try:
> >     ...
> > finally:
> >     action2(f)

> > I can't see how this would ever do something different than your version.

> Oh, the problem is that if `action1()' raises an exception (and this is
> why it has to be within the `try', not before), `f' will not receive
> a value, and so, may not be initialised in all cases.  The (frequent)
> stunt is a guard so this never becomes a problem.

in Guido's solution, the "finally" clause won't be called at all if action1 raises
an exception.

</F>





More information about the Python-Dev mailing list