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

Serhiy Storchaka storchaka at gmail.com
Tue Feb 26 10:19:00 CET 2013


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.




More information about the Python-ideas mailing list