[Python-ideas] Add lookahead iterator (peeker) to itertools

Masklinn masklinn at masklinn.net
Tue Feb 26 10:23:03 CET 2013


On 2013-02-26, at 10:19 , Serhiy Storchaka wrote:

> On 25.02.13 22:27, Antoine Pitrou wrote:
>> On Mon, 25 Feb 2013 22:21:03 +0200
>> Serhiy Storchaka <storchaka at gmail.com>
>> wrote:
>>> On 25.02.13 20:53, Antoine Pitrou wrote:
>>>>>> def test_lookahead():
>>>>>>       it = iter('abc')
>>>>>>       while True:
>>>>>>           it, peeking = itertools.tee(it)
>>>>> 
>>>>> This should be outside a loop.
>>>> 
>>>> Only if you restrict yourself to access peeking each time you access it.
>>>> (which, I suppose, is not the general use case for the lookahead
>>>> proposal)
>>> 
>>> Only if your do not want to consume O(N) memory and spend O(N**2) time.
>> 
>> No, that's beside the point. If you don't consume "peeking" in
>> lock-step with "it", then "peeking" and "it" become desynchronized and
>> therefore the semantics are wrong w.r.t to the original feature
>> request (where "peeking" is supposed to be some proxy to "it", not an
>> independently-running iterator).
> 
> Yes, of course, you should consume "peeking" in lock-step with "it".
> 
> My note is that if you create tee every iteration, this will lead to an linear increase in memory consumption and degradation of speed.

I believe I also saw situations where I blew through the recursion limit
by stacking too many tees, but I can't reproduce it right now.


More information about the Python-ideas mailing list