[Python-ideas] for/else syntax

Nick Coghlan ncoghlan at gmail.com
Thu Oct 1 14:36:59 CEST 2009

Yuvgoog Greenle wrote:
> Thank god for empirical evidence and thanks Antti for the measurement.

The plural of anecdote is not data...

Although it seems the "general loop" form in Python may need to be
better taught to make these semantics easier to remember :P

while loop_cond:
  # loop body
  # else clause

can be translated almost exactly as:

while 1:
  if loop_cond:
    # Loop body goes here
    # Else clause goes here

The for loop form is a natural extension of that (with the implicit loop
condition being "while there is stuff left in the iterable").

I can understand people not knowing what the construct means the first
time they see it. But the underlying mechanics really shouldn't be all
that difficult to grasp.


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

More information about the Python-ideas mailing list