[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.

for the change.

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.