[Python-ideas] for/else syntax

Nick Coghlan ncoghlan at gmail.com
Sat Oct 3 17:08:15 CEST 2009


Jared Grubb wrote:
> Return and yield are both
> legitimate, and technically anything that can throw an exception is too.

Nope, break is unique in that it will execute the code immediately after
the loop while not executing the code in the body of the else clause.
"yield" and "continue" won't terminate the loop at all, while "return"
and a raised exception will skip all of the code after the loop whether
it is in an else clause or not.

The *only* way to skip the else clause without also skipping the code
immediately following the loop is to use a break statement.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------



More information about the Python-ideas mailing list