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

François Pinard pinard at iro.umontreal.ca
Fri May 6 18:02:33 CEST 2005


[Guido van Rossum]

> [François Pinard]
>
> > It happens once in a while that I want to comment out the except
> > clauses of a try statement, when I want the traceback of the inner
> > raising, for debugging purposes.  Syntax forces me to also comment
> > the `try:' line, and indent out the lines following the `try:' line.
> > And of course, the converse operation once debugging is done.  This
> > is slightly heavy.

> I tend to address this by substituting a different exception. I don't
> see the use case common enough to want to allow dangling try-suites.

Quite agreed.  I just wanted to tell there was a need.

> > P.S. - Another detail, while on this subject.  On the first message
> > I've read on this topic, the original poster wrote something like:

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

> > The proposed syntax did not repeat this little part about "None",
> > quoted above, so suggesting an over-good feeling about syntax
> > efficiency.  While nice, the syntax still does not solve this
> > detail, which occurs frequently in my experience.  Oh, I do not have
> > solutions to offer, but it might be worth a thought from the mighty
> > thinkers of this list :-)

> I don't understand your issue here. What is the problem with that
> code? Perhaps it ought to be rewritten as

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

-- 
François Pinard   http://pinard.progiciels-bpi.ca


More information about the Python-Dev mailing list