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


Groups > comp.lang.python > #10543

Re: What is xrange?

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <t@jollybox.de>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.006
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'does.': 0.07; 'generators': 0.09; 'iterate': 0.09; 'tuple': 0.09; 'wrote:': 0.15; 'billy': 0.16; 'docs.': 0.16; 'iterated': 0.16; 'iterator': 0.16; 'xrange': 0.16; 'loop': 0.22; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; 'once.': 0.23; 'times,': 0.25; '(in': 0.26; '(and': 0.27; 'object': 0.30; 'complete,': 0.30; 'iterating': 0.30; 'over.': 0.30; 'subject:?': 0.31; 'print': 0.32; 'break': 0.33; 'to:addr:python-list': 0.34; 'header:User- Agent:1': 0.34; 'it?': 0.34; '(as': 0.34; 'things': 0.34; 'subject:What': 0.35; 'probably': 0.35; 'but': 0.37; 'could': 0.37; 'received:192': 0.38; 'subject:: ': 0.38; 'received:192.168.1': 0.39; 'list,': 0.39; 'goes': 0.39; 'to:addr:python.org': 0.39; 'ever': 0.65; 'received:62': 0.67; 'from:addr:t': 0.84
Date Fri, 29 Jul 2011 22:31:36 +0200
From Thomas Jollans <t@jollybox.de>
User-Agent Mozilla/5.0 (X11; Linux i686; rv:5.0) Gecko/20110628 Thunderbird/5.0
MIME-Version 1.0
To python-list@python.org
Subject Re: What is xrange?
References <j0v23e$g6o$1@speranza.aioe.org>
In-Reply-To <j0v23e$g6o$1@speranza.aioe.org>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1629.1311971484.1164.python-list@python.org> (permalink)
Lines 34
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1311971484 news.xs4all.nl 23879 [2001:888:2000:d::a6]:54493
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:10543

Show key headers only | View raw


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 | 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