[Python-Dev] Propose updating PEP 284 -- Integer for-loops

David Eppstein eppstein at ics.uci.edu
Tue Jun 21 22:51:06 CEST 2005

On 6/18/05 4:45 AM -0400 Raymond Hettinger <raymond.hettinger at verizon.net> 
> The above recommendations should get the PEP ready for judgement day.

I thought judgement day already happened for this PEP in the "Parade of 
PEPs".  No?

> Also, I recommend tightening the PEP's motivation.  There are only two
> advantages, encoding and readability.  The former is only a minor gain
> because all it saves is a function call, an O(1) savings in an O(n)
> context.  The latter is where the real benefits lay.

The readability part is really my only motivation for this.  In a nutshell, 
I would like a syntax that I could use with little or no explanation with 
people who already understand some concepts of imperative programming but 
have never seen Python before (e.g. when I use Python-like syntax for the 
pseudocode in my algorithms lectures).  The current for x in range(...) 
syntax is not quite there.

In practice, I have been using
    for x in 0, 1, 2, ... n-1:
which does not work as actual programming language syntax but seems to 
communicate my point better than the available syntaxes.

I have to admit, among your proposed options

>     for i between 2 < i <= 10: ...
>     for i over 2 < i <= 10: ...     # chained comparison style
>     for i over [2:11]: ...          # Slice style
>     for i = 3 to 10:  ...           # Basic style

I don't really care for the repetition of the variable name in the first 
two, the third is no more readable to me than the current range syntax, and 
the last one only looks ok to me because I used to program in Pascal, long 

David Eppstein
Computer Science Dept., Univ. of California, Irvine

More information about the Python-Dev mailing list