[Python-ideas] for/else syntax

Carl Johnson cmjohnson.mailinglist at gmail.com
Fri Oct 2 11:06:46 CEST 2009


I also find for:… else: unintuitive. I know if has something to do
with break, but I always forget if it means "there was no break" or
"there was a break." I support the following change:

for item in items:
   suite
else:
   #Same as before, executes only if there was no break

for item in items:
   suite
else not break:
   #Executes only if there was no break.
   #PEP-8 should deprecate the bare else in favor of this.

for item in items:
   suite
else break:
   #Executes only if there was a break

for item in items:
   suite
else None: #None is a keyword now, so why not?
   #Executes only if there were no items or in while-loops the
condition is never true


I think the latter two language changes would be welcome additions to
Python regardless of whether for-else stays the same in the ordinary
case of not. Yes, there are ways of telling if the loop never broke or
never ran the suite besides adding these grammar changes, but so what?
This a fairly common pattern, and I think it would improve readability
without hampering the programmer's mental model of Python any more
than the existing for-else does today. If anything, this would hamper
the mental model *less*.

Two-cents-ly-yours,

—Carl M. Johnson



More information about the Python-ideas mailing list