[Python-Dev] Re: PEP 276 Simple Iterator for ints

M.-A. Lemburg mal at lemburg.com
Fri Nov 16 07:00:15 EST 2001


Skip Montanaro wrote:
> 
>     >> Below is the first draft of PEP 276 "Simple Iterator for ints".
>     >> (Available at http://python.sourceforge.net/peps/pep-0276.html)
>     >>
>     >> Feel free to comment (positive, negative, bipolar, or in between ;-).
> 
> I liked "for i in 10:" at first, however, the more I see of the Haskell
> iterator syntax, the more I like it.  Given that
> 
>     * it should cause no backward compatibility issues (you can't put "..."
>       in a list now, can you?  NumPy?)
> 
>     * it completely replaces range and xrange, not just one use of it
> 
>     * it can be made to work for floats, strings and possibly other builtin
>       data types as well as ints
> 
>     * you can optimize it well
> 
> I would tend to view the int-as-iterator as a wart (a neat sort of wart, but
> a wart nonetheless), while the Haskell syntax is elegant and seems to fit in
> well with existing Python usage.

I agree with Skip.

Skip, could you tell us more about the Haskell syntax for these
things ?

Here's a slightly different approach which works now (= it doesn't
even require a PEP :-):

With the new iterator support in Python, it should be 
easily possible to write a module which exposes a singleton 
infinite sequence called e.g. "integers". Then, using the slicing
notion we already have in Python, you could write:

for i in integers[0,...,10]:
    print i

for i in integers[0:11]:
    print i

for i in integers[0:10:2]:
    print i

for i in integers[0,...,10:2]:
    print i

(or other wild combinations of slice objects and ellipsises)

The same would work for any other number type for which
slices can reasonably well express a finite sub sequence of the
possibly infinite base range, e.g. floats, natural numbers,
(finite) fields, prime numbers, rational numbers, 
Unicode characters, arrays, etc.

The above notation obviously looks very natural to 
mathematicians, not sure about Joe User though.

Hmm, seems like a nice addition for my mxNumbers package.

-- 
Marc-Andre Lemburg
CEO eGenix.com Software GmbH
______________________________________________________________________
Consulting & Company:                           http://www.egenix.com/
Python Software:                        http://www.lemburg.com/python/





More information about the Python-list mailing list