Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #10543
| Date | 2011-07-29 22:31 +0200 |
|---|---|
| From | Thomas Jollans <t@jollybox.de> |
| Subject | Re: What is xrange? |
| References | <j0v23e$g6o$1@speranza.aioe.org> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1629.1311971484.1164.python-list@python.org> (permalink) |
On 29/07/11 21:36, Billy Mays 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) > > print "===X===" > while True: > for i in x: > print i > break > else: > break > > print "===Y===" > while True: > for i in y: > print i > break > else: > break Every for loop calls gets a new iterator from the object you're iterating over. (__iter__) -- Apparently, xrange is implemented in such a way (as are lists) that you can iterate over the object many times, while each generator object (and how could it be otherwise can only be iterated over once. What is xrange(foo)? It is an object that supports list-like indices, the iterator protocol, and probably a few other things that you will find in the stdlib docs. Generators also support the iterator protocol, but that's about as far as the similarity goes (in general) - Thomas
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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