[Python-ideas] for/else syntax

Steven D'Aprano steve at pearwood.info
Wed Oct 7 16:09:26 CEST 2009

On Thu, 8 Oct 2009 12:10:00 am Gerald Britton wrote:
> Are you sure?  Under what conditions would the "finally" clause be
> bypassed?

Are you talking about the existing try...finally clause, or the 
hypothetical "let's rename for...else to for...finally" clause?

If the second, then it would be bypassed under exactly the same 
conditions as the else in for...else is bypassed. E.g.:

for x in [1, 2, 3]:
    raise Exception
    print "This is never printed"

def test():
    for x in [1, 2, 3]:
        return x
        print "This is never printed"

Renaming 'for...else' to 'for...finally' doesn't turn it into a try 
block. The semantics remain the same.

Steven D'Aprano

More information about the Python-ideas mailing list