for-else

castironpi at gmail.com castironpi at gmail.com
Wed Mar 12 15:15:09 EDT 2008


On Mar 11, 4:43 am, NickC <ncogh... at gmail.com> wrote:
> On Mar 4, 11:27 pm, bearophileH... at lycos.com 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>
>   else:
>     <0-to-1 times code block>
>     break
>
> 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>
> else:
>   <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.
>
> Cheers,
> Nick.

Here's what try: else: says.  "when control flows off the end of the
try clause."  So at least their usage is consistent, even if
pessimistic.  They might be using, "2. in addition to the persons or
things mentioned or implied: Who else was there?" - dictionary.com.

But conflate with 'if-else', the if and else are mutually exclusive.
Maybe the if-else should be if-orelse.  One's a misnomer.



More information about the Python-list mailing list