PEP 276 Simple Iterator for ints (fwd)

Marcin 'Qrczak' Kowalczyk qrczak at knm.org.pl
Sat Dec 1 07:34:01 EST 2001


Tue, 27 Nov 2001 16:36:51 -0800, David Eppstein <eppstein at ics.uci.edu> pisze:

>> Greg Ewing wrote:
>> 
>> > While trying to think of a range syntax that looks
>> > unambiguously half-open without clashing with list
>> > or tuple constructors, the following blindingly
>> > obvious solution occurred to me:
>> > 
>> >   for 0 <= i < 5:
>> >     ...
>> 
>> +1 if it also groks "for 0 < i <= 5" and so on.
> 
> Also +1 if it gives the reversed sequence for "for 5 > i >= 0".
> 
> I don't much care about nice syntax for increments other than 1 and -1.

I really like it.

It's understandable even if somebody didn't know it. It's consistent
with the meaning of '0 <= i < 5' as an expression in the same way as
'for i in s' is consistent with the 'i in s' expression. It shows which
ends are included and which are excluded better than range(). It's
resembles the mathematical notation used e.g. below the summation sign.
It's easy to implement more efficiently than range(). It's backwards
compatible.

The higher bound can be omitted: 'for i >= 0'.

If the body inside the loop rebinds the iteration variable (not a great
idea but we must give it some meaning), it's IMHO reasonable to let
it continue iteration from the new value. I.e. it's translated to
'while' rather than to 'for i in range'.

-- 
 __("<  Marcin Kowalczyk * qrczak at knm.org.pl http://qrczak.ids.net.pl/
 \__/
  ^^
QRCZAK



More information about the Python-list mailing list