for what are for/while else clauses

Magnus Lyck? magnus at thinkware.se
Sat Nov 22 12:38:12 EST 2003


"Fredrik Lundh" <fredrik at pythonware.com> wrote in message news:<mailman.795.1069077567.702.python-list at python.org>...
> the break statement has nothing to do with the else clause; the else
> is executed when there's no more item in the sequence.  if you break
> or raise or return or yield-and-never-resume or call-and-never-return
> your way out of the loop doesn't matter.

On the other hand, I only think it's in the case of a break
in block that...

while condition:
    block
else:
    print "Loop finished"

...will behave differently than...

while condition:
    block
print "Loop finished"

So, unless you use a break in the block, the else statement
is just noise: an extra line of code and additional whitespace
for the following statement(s).

It's only together with the python idiom of breaking out of
loops with ifs (which is considered bad form in some (non 
python) circles that the else part of the for and while statements 
make sense.




More information about the Python-list mailing list