[Python-ideas] Make return inside a finally a SyntaxError

Michael Foord fuzzyman at gmail.com
Sat Jul 18 13:38:53 CEST 2009


2009/7/18 Guido van Rossum <guido at python.org>

> On Fri, Jul 17, 2009 at 4:41 PM, Michael<fuzzyman at gmail.com> wrote:
> > Here are two examples of why allowing return inside a finally block is a
> bad
> > idea:
> >
> > def f():
> >  try:
> >    return 3
> >  finally:
> >    return 4
> >
> > def f():
> >  try:
> >    raise Exception()
> >  finally:
> >    return 4
>
> Well, too bad. As others have (gently) tried to point out, there will
> always remain a gray area for what should be accepted and what
> shouldn't. Feel free to put in your company's style guide that this is
> a bad idea. In most cases it probably is. But I don't think the parser
> should police this particular issue. (And were you surprised anyway?
> Since you agree that an exception raised in a finally block has
> well-defined semantics, why wouldn't a return statement?)



I was surprised - I *thought* the semantics of finally were well defined;
that exceptions inside the try block will be propagated unless a new
exception is raised inside the finally. That return (and break it
transpires) silently and implicitly swallow those exceptions is surprising.

It is also *odd*. If you *want* behavior then it is trivial to use an except
instead of a finally, so I don't see a use case for it.

All the best,

Michael


>
>
> --
> --Guido van Rossum (home page: http://www.python.org/~guido/<http://www.python.org/%7Eguido/>
> )
>



-- 
http://www.ironpythoninaction.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20090718/f67d740c/attachment.html>


More information about the Python-ideas mailing list