for what are for/while else clauses

Magnus Lyck? magnus at
Sat Nov 22 18:38:12 CET 2003

"Fredrik Lundh" <fredrik at> wrote in message news:<mailman.795.1069077567.702.python-list at>...
> 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:
    print "Loop finished"

...will behave differently than...

while condition:
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