Chad Netzer cnetzer at sonic.net
Mon Jun 23 01:27:44 CEST 2003

On Sun, 2003-06-22 at 09:34, Roman Suzi wrote:

> Interesting, what prevents from making xrange-object an iterator? Then there
> will be obvious backward compatibility and no need to change idioms.

In Python 2.3, xrange() returns an iterator in for-loops (thus, it is
faster than it used to be).

However, itertools.repeat() only needs to do an increment like this:

   i += 1

whereas, the xrange() iterator does something like this:

   i += 1
   current = start + i * step_size

which is basically why repeat() is faster.  But, if you need a step size
other than one, or are iterating over a range not starting at zero, then
xrange() has the speed and elegance advantage.

As for Bryan's original question, as for whether xrange() will be
obsoleted, the answer seems to be it won't be until/if Python 3 rolls
around, in which case the range() call will likely become a lazy range
object, and list(range()) will be used to explicitly return a list over
the specified range.  Until then, xrange() will probably continue to

There has been discussion on the development list about the possibility
of optimizing away the actual list construction in a "for i in range():"
loop.  There is even a proposed patch, but Guido has brought up some
technical objections to the current implementation.  It probably won't
happen until 2.4, if it does happen.

Chad Netzer <cnetzer at sonic.net>

More information about the Python-list mailing list