What is xrange?

Jerry Hill malaclypse2 at gmail.com
Fri Jul 29 16:36:47 EDT 2011


On Fri, Jul 29, 2011 at 3:36 PM, Billy Mays <noway at nohow.com> wrote:
> Is xrange not a generator?  I know it doesn't return a tuple or list, so
> what exactly is it?  Y doesn't ever complete, but x does.
>
> x = (i for i in range(10))
> y = xrange(10)

xrange() does not return a generator.  It returns an iterable xrange
object.  If you want the iterator derived from the iterable xrange
object, you can get it like this:  iterator = y.__iter__()

See http://docs.python.org/library/functions.html#xrange for the
definition of the xrange object.

http://www.learningpython.com/2009/02/23/iterators-iterables-and-generators-oh-my/
seems to cover the differences between iterables, iterators, and
generators pretty well.

Some more reading:
http://docs.python.org/howto/functional.html
http://www.python.org/dev/peps/pep-0255/
http://www.python.org/dev/peps/pep-0289/

-- 
Jerry



More information about the Python-list mailing list