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


Groups > comp.lang.python > #26505

Re: when an iterable object is exhausted or not

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 <python.list@tim.thechases.com>
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 <python.list@tim.thechases.com>
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 <franck@ditter.org>
Subject Re: when an iterable object is exhausted or not
References <franck-DC86E6.21203604082012@news.free.fr>
In-Reply-To <franck-DC86E6.21203604082012@news.free.fr>
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 <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.2948.1344111770.4697.python-list@python.org> (permalink)
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

Show key headers only | View raw


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


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


Thread

when an iterable object is exhausted or not Franck Ditter <franck@ditter.org> - 2012-08-04 21:20 +0200
  Re: when an iterable object is exhausted or not Tim Roberts <timr@probo.com> - 2012-08-04 12:44 -0700
    Re: when an iterable object is exhausted or not Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-05 00:47 +0000
  Re: when an iterable object is exhausted or not MRAB <python@mrabarnett.plus.com> - 2012-08-04 21:11 +0100
  Re: when an iterable object is exhausted or not Tim Chase <python.list@tim.thechases.com> - 2012-08-04 15:24 -0500
  Re: when an iterable object is exhausted or not Terry Reedy <tjreedy@udel.edu> - 2012-08-04 17:04 -0400
  Re: when an iterable object is exhausted or not Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-05 02:13 +0000

csiph-web