Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #26509
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Subject | Re: when an iterable object is exhausted or not |
| Date | 2012-08-04 17:04 -0400 |
| References | <franck-DC86E6.21203604082012@news.free.fr> <501D84E2.6000404@tim.thechases.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2950.1344114320.4697.python-list@python.org> (permalink) |
On 8/4/2012 4:24 PM, Tim Chase wrote:
> 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.
range produces a re-iterable range object because it can. The result is
self-contained with 3 data attributes, so it can create rangeiterators
on demand.
filter, on the other hand, depends on an external iterable and it cannot
depend on that external object being re-iterable. So even if we
programmed filter() to produce a filter object that produced
filteriterators, the latter would often only work for the first. Also,
If you want the filtered collection more than once, you should just save
it. On the other hand, reproducing counts with a rangeiterator is nearly
as fast as looking them up in a saved list, and much more memory efficient.
--
Terry Jan Reedy
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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