"Weird" Indentation? (Or: is there a for...else construct?)

Steven D'Aprano steve at pearwood.info
Sat Feb 7 09:28:00 EST 2009


Andreas Waldenburger wrote:

> It seems that there is a for...else construct. Replacing the inner if
> with pass seems to confirm this. The else clause is still executed.

Yes, there is a for...else construct.

The else block runs if the for loop exits *without* a break.

for i in range(20):
    if i == 10: break
else:
    print "no break here"

for i in range(20):
    if i == 100: break
else:
    print "no break here"


> What's broken here: Python or my brain?

Perhaps we should not answer that question.


-- 
Steven




More information about the Python-list mailing list