1.5.2 for: else:

Fredrik Lundh fredrik at pythonware.com
Tue Jul 27 20:50:14 EDT 1999


William Tanksley <wtanksle at dolphin.openprojects.net> wrote:
> The very worst part of the current else: behavior is that it changes the
> meaning of else.  In other constructs, else is an alternate path to take
> if the data being tested fails a single expression test.  In this
> contruct, else is a path taken if the code block belonging to the previous
> test executes a certain instruction.

nope.  you've got it all backwards.  consider this:

    for an "if" statement, "else" is a path taken if the
    expression evaluates to false.

    for a "while" statement, "else" is a path taken if the
    expression evaluates to false.  or in other words,
    when the loop terminates by natural causes.

    for a "for" statement, "else" is a path taken when
    there are no more elements to loop over.  or
    in other words, when the loop terminates by
    natural causes.

not that complicated, was it?

now, what you seem to have trouble with isn't
the "else"-clause -- it's the behaviour of "break":

    "break" doesn't just break out of the inner
    body (the portion between while/for and
    else), it breaks out of the ENTIRE construct.
    including the "else" clause.  just as the docs
    say.

"else" always works the same way.  and so does
"break".  what else did you expect from Guido?

</F>





More information about the Python-list mailing list