Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #10544

Re: What is xrange?

References <j0v23e$g6o$1@speranza.aioe.org>
Date 2011-07-29 16:36 -0400
Subject Re: What is xrange?
From Jerry Hill <malaclypse2@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.1631.1311971809.1164.python-list@python.org> (permalink)

Show all headers | View raw


On Fri, Jul 29, 2011 at 3:36 PM, Billy Mays <noway@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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

What is xrange? Billy Mays <noway@nohow.com> - 2011-07-29 15:36 -0400
  Re: What is xrange? harrismh777 <harmar@member.fsf.org> - 2011-07-29 14:47 -0500
  Re: What is xrange? Thomas Jollans <t@jollybox.de> - 2011-07-29 22:31 +0200
  Re: What is xrange? Jerry Hill <malaclypse2@gmail.com> - 2011-07-29 16:36 -0400
  Re: What is xrange? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-07-30 11:22 +1000
    Re: What is xrange? Brian Blais <bblais@bryant.edu> - 2011-07-30 06:23 -0400
      Re: What is xrange? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-07-30 21:36 +1000
        Re: What is xrange? Brian Blais <bblais@bryant.edu> - 2011-07-30 09:46 -0400
        Re: What is xrange? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2011-07-30 17:06 -0700
        Re: What is xrange? Chris Angelico <rosuav@gmail.com> - 2011-07-31 01:10 +0100
        Re: What is xrange? Ethan Furman <ethan@stoneleaf.us> - 2011-07-30 23:42 -0700
      Re: What is xrange? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2011-07-31 12:18 +1200

csiph-web