PEP 276 Simple Iterator for ints (fwd)

Lulu of the Lotus-Eaters mertz at gnosis.cx
Wed Nov 14 05:26:54 EST 2001


>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]

James_Althoff at i2.com wrote:
|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.

Basically true.  If the step "overshoots" the sequence end, the final
element is excluded.  So 1.2 is not included in the last example (nor is
1.0).  But in the very frequent "count integers by ones" case, the
interval is closed.

|it appears that this syntax creates an actual list, not an iterator.

This is not quite true *in* Haskell.  Since Haskell is lazy, lists are
themselves much more like iterators.  They only get filled in as needed.
But it is not obvious how that might translate to Python.  One
possibility is to build a complete list (like 'range()' does).  But one
could also let the dots signal that an iterator is being built.  Well,
there could be problems.  I'm not sure if that works with the parser,
and this would also seem to require that iterators become indexable (and
sliceable?!).

FWIW, my own feeling is that PEP 276 is not a good idea.  The fact that
integers start acting different in scalar and iterator contexts is
troublesome.  And even though 'xrange()' is easy to forget, it is at
least explicit about the fact that integers are being listed.  The
construct like:

   for i in table.rowCount():

is overly subtle, IMO.  One might intuitively expect it to return actual
"row" objects or something like that.  Using one of the syntaxes that
explicitly list numbers is less likely to cause confusion.

Yours, Lulu...




More information about the Python-list mailing list