
Le Tue, 26 Feb 2013 11:19:00 +0200, Serhiy Storchaka <storchaka@gmail.com> a écrit :
On 25.02.13 22:27, Antoine Pitrou wrote:
On Mon, 25 Feb 2013 22:21:03 +0200 Serhiy Storchaka <storchaka@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.
Apparently, itertools.tee() is optimized for tee'ing a tee:
it = iter("abc") id(it) 31927760 it, peeking = itertools.tee(it) id(it) 31977128 it, peeking = itertools.tee(it) id(it) 31977128
Regards Antoine.