PEP 284, Integer for-loops

Delaney, Timothy tdelaney at avaya.com
Wed Mar 6 19:44:52 EST 2002


> From: David Eppstein [mailto:eppstein at ics.uci.edu]
> 
> When you say "incorrect", do you mean that "making integers 
> iterable" is 
> different from "allowing automatic conversion of integers to 
> iterators"?

Yes.

> It was my understanding that "for x in y" and "if x in y" 
> converted y to 
> an iterator automatically, by calling iter(y) if necessary.  Integers 
> can be made iterable now, by calling range() on them, but that is not 
> automatic.

That's automatic, but not a *conversion*. It is asking the iterable object
to *supply* an iterator. Calling range() on an integer does not make the
integer iterable ... it creates an (iterable) list using the integer as one
of the bounds.

> In what way would you propose rewording the PEP?  If it's 
> just "PEP 274 
> [4] proposes to make integers iterable, simplifying..." that seems 
> unobjectionable enough.

Or possibly make it more explicit ...

"PEP 274 [4] proposes to make integers iterable by implementing __iter__,
simplifying ...

I would also possibly add the discussion of what iter(-x) should mean i.e.
either counting up from -x to 0 (upper bound exclusive, matches slice
semantics), or counting down from 0 to -x (upper bound exclusive, useful for
traversing sequences in reverse order).

Tim Delaney




More information about the Python-list mailing list