NickC ncoghlan at
Tue Mar 11 10:43:29 CET 2008

On Mar 4, 11:27 pm, bearophileH... at wrote:
> The meaning is explicit. While "else" seems to mean little there.
> So I may like something similar for Python 3.x (or the removal of the
> "else").

Consider a loop with the following form:

while 1:
  if <while-cond>:
    <0-to-many times code block>
    <0-to-1 times code block>

A break, return or exception in the 0-to-many times code block will
obviously skip over the 'else' part of that if statement - it will
only be executed if <while-cond> evaluates as a false value. The above
code is actually equivalent to a normal Python while-loop:

while <while-cond>:
  <0-to-many times code block>
  <0-to-1 times code block>

For loops aren't quite so straightforward since the termination
condition is tied up with the StopIteration exception, but the clause
keeps the same name as the corresponding clause on the while loop.
Thinking of it as break-else (as someone else posted) probably isn't a
bad way to look at the situation.


More information about the Python-list mailing list