PEP 276 Simple Iterator for ints (fwd)
David Eppstein
eppstein at ics.uci.edu
Thu Dec 6 20:50:55 EST 2001
In article <mailman.1007685070.25864.python-list at python.org>,
James_Althoff at i2.com wrote:
> I agree with David. I think there should be an iterator, lightweight
> though it might be.
>
> for x <= i < y: # alt. spelling of: for i in xrange(x,y)
> for x <= i <= y: # alt. spelling of: for i in xrange(x,y+1)
> for x < i < y: # alt. spelling of: for i in xrange(x+1,y)
> for x < i <= y: # alt. spelling of: for i in xrange(x+1,y+1)
>
> (not counting the sys.maxint boundary case).
Also not counting the possibility that x or y are noninteger.
> In any case, it would be nice for the syntax to support the creation of an
> interval-like object that I could pass around, save, and use later (in a
> for-loop, list function, "in" statement, or any other iterator-based
> context).
It seems like the choices are
(1) concise for-loop, verbose interval-like object
for x <= i < y
L = [i for x <= i < y]
(2) verbose for-loop, concise interval-like object
for i in x <= ... < y
L = (x <= ... < y)
These are not quite the same because of the list/iterator distinction but
the problem that list comprehensions can't be made to return iterators
instead of lists seems to be a more general one than this.
--
David Eppstein UC Irvine Dept. of Information & Computer Science
eppstein at ics.uci.edu http://www.ics.uci.edu/~eppstein/
More information about the Python-list
mailing list