PEP 276 Simple Iterator for ints (fwd)

James_Althoff at i2.com James_Althoff at i2.com
Fri Dec 7 15:24:24 EST 2001


David Eppstein wrote:
>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.

Right.  (Although the second line of (2) was probably meant to be

    L = list(x <= ... < y)

Here's a simplistic example of how one might make use of intervals as
arguments to functions or methods:

>>> def printSquares(interval):
...   for i in interval:
...     print i, i*i
...
>>> printSquares(2 <= ... <= 5)  # faked it by using // & span
2 4
3 9
4 16
5 25
>>> printSquares(2 <= ...(step=2) <= 10)  # faked it by using // & span
2 4
4 16
6 36
8 64
10 100
>>>

Jim





More information about the Python-list mailing list