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

andrew cooke andrew at acooke.org
Sat Feb 7 09:42:05 EST 2009


there's a justification for this awful mess here -
http://mail.python.org/pipermail/python-3000/2006-March/000104.html

i didn't know about this, and even after reading steven's broken (i
assume) example, managed to get it backwards.

the else is if there *isn't* a break and is for search loops (see link
above).

(it's still in 3).

andrew


Steven D'Aprano wrote:
> 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
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>





More information about the Python-list mailing list