[Python-ideas] Add lookahead iterator (peeker) to itertools
Antoine Pitrou
solipsis at pitrou.net
Mon Feb 25 21:27:22 CET 2013
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).
Regards
Antoine.
More information about the Python-ideas
mailing list