Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '---------': 0.05; 'behavior,': 0.09; 'subject:iterable': 0.09; 'cc:addr:python- list': 0.10; 'programmer': 0.11; 'subject:not': 0.11; '(x,': 0.16; '-tkc': 0.16; 'exhausted': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'iterable': 0.16; 'iterator': 0.16; 'message- id:@tim.thechases.com': 0.16; 'rationale': 0.16; 'received:70.251': 0.16; 'received:dsl.rcsntx.swbell.net': 0.16; 'received:rcsntx.swbell.net': 0.16; 'received:swbell.net': 0.16; 'subject:object': 0.16; 'subject:when': 0.16; 'wrote:': 0.17; '>>>': 0.18; "i'd": 0.22; 'cc:2**0': 0.23; 'seems': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In- Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'guess': 0.27; 'forces': 0.29; 'objects': 0.29; 'print': 0.32; "can't": 0.34; 'similar': 0.35; 'but': 0.36; 'compare': 0.36; 'should': 0.36; 'two': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'speak': 0.38; 'some': 0.38; 'different': 0.63; 'behavior': 0.64; '$$$': 0.84; '(your': 0.84; 'received:50.22': 0.84 Date: Sat, 04 Aug 2012 15:24:02 -0500 From: Tim Chase User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.24) Gecko/20111120 Icedove/3.1.16 MIME-Version: 1.0 To: Franck Ditter Subject: Re: when an iterable object is exhausted or not References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - boston.accountservergroup.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tim.thechases.com X-Source: X-Source-Args: X-Source-Dir: Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 43 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1344111770 news.xs4all.nl 6969 [2001:888:2000:d::a6]:60217 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:26505 On 08/04/12 14:20, Franck Ditter wrote: > Two similar iterable objects but with a different behavior : > > $$$ i = range(2,5) > $$$ for x in i : print(x,end=' ') > > 2 3 4 > $$$ for x in i : print(x,end=' ') # i is not exhausted > > 2 3 4 > > --------- Compare with : > > $$$ i = filter(lambda c : c.isdigit(), 'a1b2c3') > $$$ for x in i : print(x,end=' ') > > 1 2 3 > $$$ for x in i : print(x,end=' ') # i is exhausted > > $$$ > > IMHO, this should not happen in Py3k. > What is the rationale of this (bad ?) design, which forces the programmer > to memorize which one is exhaustable and which one is not ?... I can't speak to the rationale, but it seems that a range() object has some extra features that a normal iter doesn't: >>> i = iter(range(2,5)) >>> for x in i: print (x, end=' ') ... 2 3 4 >>> for x in i: print (x, end=' ') ... (your 2nd behavior, and what I'd expect). So my guess would be that the "for {var} in {thing}" triggers a re-calling of range.__iter__ since it's not an iterator to begin with. -tkc