[Python-ideas] for/else syntax

Nick Coghlan ncoghlan at gmail.com
Fri Oct 2 13:14:00 CEST 2009

Antti Rasinen wrote:
> It seems to me that the only sensible use case for for..else is when
> there is an conditional break inside the loop. I'll appreciate any
> counter examples, of course.

Nope, as far as I know, that's what the construct is for - search loops.

> It seems to me that there often are "more intuitive" ways of writing a
> for..else-loop. And accordingly, the construct is rare. I'll resort to
> some newspeak here and define "intuitive" as either "conveys the intent
> of the programmer better" or as "feels like the more obvious way to do
> it". Pick either.

I'd agree. However, given that it is an existing construct with valid
(albeit fairly uncommon) use cases, I don't see much point in messing
with it.

Encouraging people to refer to the idiom as "break/else" and including a
SyntaxWarning for dubious usage of loop else clauses (i.e. those without
an associated break statement) are probably two fairly low cost
educational steps that could be taken though.


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-ideas mailing list