
2009/7/18 Guido van Rossum <guido@python.org>
On Fri, Jul 17, 2009 at 4:41 PM, Michael<fuzzyman@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/> )