PEP 276 Simple Iterator for ints

Ken Seehof kseehof at neuralintegrator.com
Thu Nov 15 19:29:44 CET 2001


Hey, what ever happened to the int[:10] iterator idea?

>>> for i in int[2:10:2]: print i,
2 4 6 8

Like PEP 276, it doesn't require new syntax (just implement a slice operator
for int).

int[3:8] should return an iterator equivalent to [3,4,5,6,7]

int[-4::2] should return an infinite sequence iterator [-4, -2, 0, 2, ...]

I think that this solves all of the problems that PEP 276 solves, without
any inconsistent idioms or warts.


Here's what http://python.sourceforge.net/peps/pep-0276.html has to
say about it:

    - It would be better to reuse the slicing literal syntax attached
      to the int class, e.g., int[0:10]

      Response: Same as previous response.

        """ Response: Shares disadvantages of other proposals that require
         changes to the syntax.  Needs more design to determine how it
         would handle the general case of start,stop,step,
         open/closed/half-closed intervals, etc.  Needs a PEP."""

      In addition, design
      consideration needs to be given to what it would mean if one
      uses slicing syntax after some arbitrary class other than class
      int.  Needs a PEP.

What syntax change?  There is no syntax change.  A slice operator would
simply be added to the int class.  Slicing syntax applied to some arbitrary
class would naturally depend on the slice implementation for that class :-)
I agree that it needs a PEP.

Hey, you could also do floats!

>>> for x in float[ :1.0 : 0.2]: print x,
0.0 0.2 0.4 0.6 0.8
>>> list(float[3.0:1.0:-0.5])
[3.0, 2.5, 2.0, 1.5]

Hmm.  Complex numbers?  Of course the step would be in radians :-)  heh heh.

- Ken






More information about the Python-list mailing list