1.5.2 for: else:

William Tanksley wtanksle at dolphin.openprojects.net
Thu Jul 29 15:48:49 EDT 1999


On Wed, 28 Jul 1999 20:11:10 GMT, Fredrik Lundh wrote:
>William Tanksley <wtanksle at dolphin.openprojects.net> wrote:
>> >    for an "if" statement, "else" is a path taken if the
>> >    expression evaluates to false.

>> Close enough -- I would say it's the path taken if the 'if' block isn't
>> executed.

>you can say what you want, but it isn't defined
>that way; the language reference says:

>    "If all expressions are false, the suite of
>    the else clause, if present, is executed." 
>    (where "all expressions" are the expressions
>    for all if/elif clauses in the statement)

>if you continue reading, you'll find:

>    "if the expression is false (which may be
>    the first time it is tested) the suite of the
>    else clause, if present, is executed"

>and

>    "when the items are exhausted (which is
>    immediately when the sequence is empty),
>    the suite in the else clause, if present, is
>    executed"

>for ALL these statements, the else clause is exe-
>cuted if and only if ALL controlling expressions
>evaluate to FALSE.

But that's not even superficially true for the current behavior of a loop.
A loop's else clause is executed even when there were TRUE results of the
controlling expressions.

In fact, the loop's else clause is executed when a single FALSE is
returned from an often hidden evaluation.

>huh?  the "else" clause isn't involved in the loop's
>termination at all; it's executed when the con-
>trolling expression becomes false.  if you raise
>an exception, or use the break statement, or
>return to the caller, you don't end up in the
>"else" clause by a very simple reason:  the con-
>trolling expression isn't false.

This actually makes a lot of sense for a while statement, since there's an
explicit single-valued controlling expression.  If the while statement
were the only loop statement with an else, I wouldn't bother arguing about
it; I would just think that I would rather not use a break for that
purpose (but scripting languages are wonderful that way, you get to write
code that makes sense to YOU, not to me or Guido).

>I repeat:

>for ALL these statements, the else clause is exe-
>cuted if and only if ALL controlling expressions
>evaluate to FALSE.

Except 'for', where it executes if and only if _one_ of the "controlling
expressions" (all of which are implicit) evaluates to FALSE.

If it truly was consistent with your own definition, then it would execute
if and only if none of the controlling expressions ever evaluated to TRUE
-- in other words, if the for loop never iterated.

I think I got you by your very own words.

>you can argue as much as you want, but that's the
>way it is.  you gotta live with it (unless you have your
>own time machine, of course ;-)

I agree with this, of course :).

I'm nore agitating for Python 2.0.  But then I'd be happy to see else go
away or stay the same, too; it's too minor of a feature.  I guess, then,
that I'm merely defending my point of view.

></F>

-- 
-William "Billy" Tanksley




More information about the Python-list mailing list