[Python-Dev] xrange identity crisis
Jeff Epler
jepler@unpythonic.net
Tue, 4 Jun 2002 16:01:24 -0500
On Tue, Jun 04, 2002 at 04:08:08PM -0400, Oren Tirosh wrote:
> It seems that the xrange object in the current CVS can't make up its mind
> whether it's an iterator or an iterable:
In 2.2, xrange had no "next" method, so it got wrapped by a generic
iterator object. It was desirable for performance to have xrange also
act as an iterator.
See
http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/python/python/dist/src/Objects/rangeobject.c.diff?r1=2.35&r2=2.36
for the change.
See
http://www.python.org/sf/551410
for the sf patch this comes from.
However, the following code would give different results if 'iter(x)
is x' for xrange objects:
x = xrange(5)
for a in x:
for b in x:
print a,b
(it'd print "0 1" "0 2" "0 3" "0 4" if they were the same iterator, just
as for 'x = iter(range(5))') so, it's necessary to return a *different*
xrange object from iter(x) so it can start iterating from the beginning
again. I think there's an optimization that *the first time*, iter(x)
is x for an xrange object.
Hm, the python cvs I have here is too old to have this optimization ...
so I can't really tell you how it works now for sure.
Jeff