PEP 276 Simple Iterator for ints

James_Althoff at i2.com James_Althoff at i2.com
Wed Nov 14 14:40:39 EST 2001


[mertz at gnosis.cx/Lulu]
>Well... here's how Haskell does it:
>
>  [1 .. 4]         --> [1,2,3,4]
>  [4,3 .. 0]       --> [4,3,2,1,0]
>  [2,4 .. 10]      --> [2,4,6,8,10]
>  [0.0,0.3 .. 1.0] --> [0.0,0.3,0.6,0.9]
>
>It's hard to think of a better syntax for Python to use.

[Skip Montanaro]
>Though after reading the remainder of the thread, I like the Haskell
syntax
>best.

[Rainer Deyke]
>for i in [g() + 0, g() + 1, ... g() + 13]: pass
>
>How often is 'g' called?  Once?  3 times?  14 times?

[David Eppstein]
My assumption would be that the syntax [x, y, ... z] is equivalent to
range(x,z+y-x,y-x).  This answers all of your questions:

[Tim Hochberg]
>So you can use it in your own class if you want. In particular, one could
>make
>
>int[1,3,...,53] act like range(1,53,2) if one wanted.

[Gareth McCaughan]
>  - I agree that Python needs a better way of doing
>    simple iterations over sequences of numbers. I'm
>    concerned that if this PEP is accepted then it may
>    discourage the implementation of something clearer.
>
>  - For all that, the proposal *is* quite elegant, and
>    the syntax it offers is certainly concise. And if
>    numbers are going to provide iterators, James has
>    chosen the right ones.

[Jim]
The Haskell syntax does seem a nice way to specify some sequences of
integers.  Rainer points out some possible issues when one uses functions
rather than literals in conjunction with said syntax (and David suggests
answers).

A couple of points: it appears that the Haskell syntax mentioned above
assumes a closed interval.  This means that in the common case of indexing
a sequence, one must remember to subtract 1 from the ending value.
E.g.,
    for i in [0 .. len(mysequence)-1]:
        mysequence[i]
Also, it appears that this syntax creates an actual list, not an iterator.
So presumably, that leaves unresolved the range/xrange problem (i.e., I
have a huge set of indices that I want to iterate through without first
having to create a huge list).

Getting back to the motivating example of PEP 276 -- as stated in the PEP,
adding an iterator to types.IntType would allow one to write:

for rowcount in table.getRowCount():

Summarizing some of the other possibilities, one might write:

for rowcount in range(table.getRowCount()):   # valid in current Python
for rowcount in xrange(table.getRowCount()):  # valid in current Python

for rowcount in [0 .. table.getRowCount()-1]:  # new Haskell-esque syntax
for rowcount in 0...table.getRowCount():  # new syntax using elipsis
for rowcount in [0:table.getRowCount()]:  # new syntax proposed in PEP 204
for rowcount in int[0:table.getRowCount()]:  # __getslice__ as class method
in types.IntType (no new syntax)

Stating again that PEP 276 does not propose any new syntax for Python, it
should be noted that adding an iterator to types.IntType as proposed in PEP
276 does *not* preclude one from inventing some new syntax for specifying
sequences of integers (such as the examples above).  (It might lessen the
motivation to do so as noted by Gareth.  OTOH, decreasing the motivation
for adding new syntax might not be such a bad thing ;-)

And again, with any proposal for creating sequences of integers, there is
the issue of how one specifies the creation of an *iterator* (which is what
one actually wants in a for-loop as of Python 2.1) versus the creation of
an actualized *list*.  PEP 276 addresses this issue specifically.

Jim







More information about the Python-list mailing list