1.5.2 for: else:

Hans Nowak ivnowa at hvision.nl
Tue Jul 27 18:09:29 EDT 1999


On 27 Jul 99, Oleg Broytmann wrote:

> > Loop statements may have an else clause; it is executed when the loop
> > terminates through exhaustion of the list (with for) or when the
> > condition becomes false (with while), but not when the loop is
> > terminated by a break statement. [..]
> [skip]
> > Else clauses get executed on normal loop exit, even if the loop was
> > 'empty'. It does not get executed when you 'break' out of a loop.
> 
>    Aha, thanks. I expected "else" to be executed when "for" not executed
>    on
> empty list. Wrong assumption.

This one has bitten me too in the past. One of the few non-intuitive 
parts of Python, methinks. :(

--Hans Nowak (zephyrfalcon at hvision.nl)
Homepage: http://fly.to/zephyrfalcon
Python Snippets: http://www.hvision.nl/~ivnowa/snippets/
The Purple Kookaburra Forum: http://www.delphi.com/kookaburra/




More information about the Python-list mailing list