[Python-ideas] for/else syntax

Stephen J. Turnbull stephen at xemacs.org
Thu Oct 1 15:37:09 CEST 2009

Antti Rasinen writes:

 > never used it. Three uninitiated, in other words.

Well, of course it's unintuitive, then.  In practical programming,
"unintuitive" is synonymous with "I've never used it in a real

 > Here are their reactions, when they learned how it really works:
 >   * "It should be called for..then" (or for..finally)

I don't know about the semantics I'd guess for "then", but I think I'd
consider that synonymous with "finally".  IMO it's non-starter anyway
because it's not already a keyword; this pattern is a little too
uncommon to justify a new keyword.  "finally" is no good because in
try blocks it means "no matter what, do this".  Having slightly
different semantics in try blocks and loops would be a real loser.

 > To add further insult to the naming injury, the else branch suffers from
 > the fact that 99.9% of the time, the for-loop contains an if:
 > for x in xs:
 >     if cond(x):
 >         break
 >     # stuff
 > else:
 >     # more stuff
 > My code pattern matching algorithm reads that as an indentation error. An
 > else matches if so many times more often that decoding for..else requires
 > unnecessary effort.

I guess.  However, you're a native speaker of a natural language, so I
bet you can deal with idioms that are *much* less intuitive than this
one, if used in context.  Probably even in English (which I guess is
not your native language).  I find the search idiom very natural,
almost intuitive (even though I've never used it myself<wink>), now
that I've seen it in Nick's post.  Don't you?

More information about the Python-ideas mailing list