PEP 276 Simple Iterator for ints (fwd)

James_Althoff at i2.com James_Althoff at i2.com
Thu Dec 6 19:29:48 EST 2001


Greg Ewing wrote:
>David Eppstein wrote:
>>
>> if "for x in list" doesn't change subsequent iterations if you
>> rebind x or list within the loop body, neither should "for x <= i < y"
>> or whatever similar syntax one uses. So that means, it should construct
an
>> iterator and then act as if it were called by "for i in iterator"
>
>I think relying on the semantics of changing the
>loop variable in a loop is a bad idea in the first
>place, so the difference wouldn't worry me.
>
>In any case, you can get the same semantics quite
>easily without the need for an iterator: keep the
>real loop counter in a hidden variable, and assign
>it to the user's loop variable before each iteration.

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).

or

# same meaning:
for i in x <= ... < y:
for i in x <= ... <= y:
for i in x < ... < y:
for i in x < ... <= y:

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).

Jim





More information about the Python-list mailing list