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


Groups > comp.lang.python > #26504

Re: when an iterable object is exhausted or not

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 <python@mrabarnett.plus.com>
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 <python@mrabarnett.plus.com>
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 <franck-DC86E6.21203604082012@news.free.fr>
In-Reply-To <franck-DC86E6.21203604082012@news.free.fr>
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 <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.2947.1344111131.4697.python-list@python.org> (permalink)
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

Show key headers only | View raw


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)
<range_iterator object at 0x0135DB30>

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
<filter object at 0x01360B30>

The 'for' loop passes it to 'iter' to get an iterator:

 >>> iter(i)
<filter object at 0x01360B30>

More importantly:

 >>> iter(i) is i
True

In other words, the 'filter' object returns _itself_ as the iterator.

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