Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.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.014 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; '---------': 0.05; 'subject:iterable': 0.09; 'programmer': 0.11; 'subject:not': 0.11; 'exhausted': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'iterable': 0.16; 'iterator,': 0.16; 'iterator.': 0.16; 'iterator:': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'rationale': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'subject:object': 0.16; 'subject:when': 0.16; 'wrote:': 0.17; 'passes': 0.17; 'creates': 0.18; '>>>': 0.18; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; 'forces': 0.29; 'received:192.168.1.3': 0.29; 'objects': 0.29; 'received:84': 0.32; 'asked': 0.33; 'to:addr:python-list': 0.33; 'false': 0.35; 'similar': 0.35; 'but': 0.36; 'compare': 0.36; 'should': 0.36; 'two': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'different': 0.63; 'more': 0.63; 'behavior': 0.64; 'header:Reply-To:1': 0.68; 'reply-to:no real name:2**0': 0.72; '$$$': 0.84; "'for'": 0.84; "'range'": 0.84; 'object:': 0.84; 'reply-to:addr:python.org': 0.84; 'hand,': 0.97 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.0 cv=IekFqBWa c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=DKcI9XZsuF4A:10 a=hf8P5ZeXepQA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=z1zGMG0xGHOWNHWc_osA:9 a=wPNLvfGTeEIA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Sat, 04 Aug 2012 21:11:56 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20120713 Thunderbird/14.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: when an iterable object is exhausted or not References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: python-list@python.org 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: 63 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1344111131 news.xs4all.nl 6976 [2001:888:2000:d::a6]:55529 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:26504 On 04/08/2012 20: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 ?... > 'range' returns a 'range' object: >>> i = range(2,5) >>> i range(2, 5) The 'for' loop passes it to 'iter' to get an iterator: >>> iter(i) More importantly: >>> iter(i) is i False In other words, when asked for an iterator, the 'range' object always creates a new one. On the other hand, 'filter' returns a 'filter' object: >>> i = filter(lambda c : c.isdigit(), 'a1b2c3') >>> i The 'for' loop passes it to 'iter' to get an iterator: >>> iter(i) More importantly: >>> iter(i) is i True In other words, the 'filter' object returns _itself_ as the iterator.