Status of PEP's?

James_Althoff at i2.com James_Althoff at i2.com
Mon Mar 4 19:33:56 EST 2002


[James_Althoff]
> I was thinking in a more general sense: I would like to create an
iterator
> with start, end, and step values and pass it into a function without
first
> actualizing it as a list (like one would have to do using for+relational
in
> conjunction with list comp.s).  And use the same construct in for-loops,
> list comp.s, etc.  (For those just tuning in: I realize that I can create
a
> class for this -- actually, I already have.  I'm refering to proposals
for
> new idioms in Python for iterating intervals of integers.)

[Jeff Shannon]
> Um...  isn't this exactly what xrange() does?  Or am I missing something
> again?

Don't know what you missed before <wink> but here I meant to say that if
syntactic changes are introduced to Python such that one doesn't have to
use range/xrange in for-loops (in situations where currently one does) then
it would be nice from my perspective for said addition of syntax to allow
one to do what is stated above (so to speak <wink>).

One could also note that in current Python the result of xrange is not
considered to be (in the strictest sense) an iterator.

>>> from __future__ import generators
>>>
>>> class spam:
...     def __iter__(self):
...         return xrange(5)
...
>>> for i in spam():
...     print i
...
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: __iter__ returned non-iterator of type 'xrange'
>>>

(It might be easy to change this by putting a test in the iter builtin?)

Jim





More information about the Python-list mailing list