[Python-Dev] xrange identity crisis
Guido van Rossum
guido@python.org
Tue, 04 Jun 2002 17:18:39 -0400
[Raymond Hettinger]
> Xrange was given its own tp_iter slot and now runs as fast a range.
> In single pass timings, it runs faster. In multiple passes, range
> is still quicker because it only has to create the PyNumbers once.
>
> Being immutable, xrange had the advantage that it could serve as its
> own iterator and did not require the extra code needed for list
> iterators and dict iterators.
Did you write the pach that Martin checked in?
It's broken.
>>> a = iter(xrange(10))
>>> for i in a:
print i
if i == 4: print '*', a.next()
0
1
2
3
4
* 0
5
6
7
8
9
>>>
Compare to:
>>> a = iter(range(10))
>>> for i in a:
print i
if i == 4: print '*', a.next()
0
1
2
3
4
* 5
6
7
8
9
>>>
--Guido van Rossum (home page: http://www.python.org/~guido/)