[Python-3000] else-clause on for-loops

Tim Peters tim.peters at gmail.com
Fri Mar 24 17:38:00 CET 2006


[Nicola Larosa]
> ... I sometimes may have had a need for the current semantics
> of the else after loops, but I don't remember it; on the other hand, I have
> had a use for the no-iteration case a number of times. Somehow I find it
> hard to stick into my mind that's not what it means.

The primary use case is "search loops".

for item in sequence:
    if desirable(item):
        break
else:
    no desirable item exists

Just remember "search loop", and you'll never be surprised again.  Now
go back and recode your search loops in this simpler way ;-)


More information about the Python-3000 mailing list