PEP 276 Simple Iterator for ints (fwd)

Christopher A. Craig com-nospam at ccraig.org
Tue Dec 11 17:32:50 CET 2001


David Eppstein <eppstein at ics.uci.edu> writes:

> On 12/11/01 8:26 AM -0500, Christopher A. Craig wrote:
> > That looks quite clear to me.  A heck of a lot clearer than
> >
> > for i in y > i >= x: print i
> >
> > Which to me does not specify the step size for floats.  (If y=5 and
> > x=0, shouldn't that print 3.1415926 at some point?)
> 
> I said, the INTEGERS between x and y.


I know it's what you said you wanted, but it's not what y > i >= x
indicates for a float.  Sure, the only reasonable default step size
for floats is 1, but that doesn't mean I have to accept that y > i >= x for
floats y and x means "integers such that y > i >= x".  Your loop 

for i in y > i >= x: print i

Means "For all integers 'i' starting below y and ending greater than
or equal to x, print i".  My point is that the syntax:

i = floor(y)
while i >= x:
 print i
 i -= 1

makes a whole lot more sense.  It explicitly specifies both that you
want integers and what the step size is, while your syntax does not.
I agree that if there were only foreach style "for" loops in Python
then your proposed syntax would be very elegant (though I would still
rather see the bounds cast to integers to make it clear we are in the
integer domain), but luckily we have "while" loops, which fit the
problem domain you seem to be addressing much better.

I may be wrong, there may be some class of problems where you really
want to use a foreach style "for" loop to iterate over a set of
integers (other than a half-open, increment by one range for sequence
indexing), but I can't think of what that might be.

-- 
Christopher A. Craig <com-nospam at ccraig.org>




More information about the Python-list mailing list