PEP 276 Simple Iterator for ints

James_Althoff at i2.com James_Althoff at i2.com
Thu Nov 15 20:19:04 CET 2001


Ken Seehof wrote:
>Hey, what ever happened to the int[:10] iterator idea?
>
>>>> for i in int[2:10:2]: print i,
>2 4 6 8

Nothing actually *happened* to it. ;-)

It needs a PEP (or needs to be added to an existing PEP).

Among the issues that the PEP should probably address are:

- Does int[] return a list or an iterator?  (Or an iterator that implements
some or all of the sequence protocol?)

A list would be useful in many cases and would be a bit more consistent in
the sense that slicing applied to current builtins returns actualized
sequences not iterators.

But iterators are more useful for for-loops (because you don't have worry
about generating large sequences unnecessarily).

So both are useful.  Do we need two mechanisms?  What would be the proposal
for that?

- Does int[] do anything more than act as a new spelling for the "range"
function?  (Or "xrange", depending on the answer to the issue above).

- Issues from PEP 204 "Range Literals".  PEP 204 proposed using slice
syntax to create lists of integers.  That PEP has been rejected.  It
included some issues such as: how do you handle [:] which is valid slicing
syntax but contains no end value?  Because the "int[]" proposal is less
drastic than PEP 204 -- calling for the implemetation of __getslice__ as a
class method on types.IntType rather than new syntax -- most of the issues
in PEP 204 are probably not a big deal.  But the PEP author should probably
give them consideration.


Finally, it should be noted that PEP 276 -- adding an iterator to
types.IntType -- doesn't preclude implementing __getslice__ as a class
method of types.IntType as suggested above.

Jim






More information about the Python-list mailing list