[Python-ideas] a memory-efficient variant of itertools.tee

Stephan Houben stephanh42 at gmail.com
Fri May 26 14:45:51 EDT 2017


Hi all,

The itertools.tee function can hold on to objects "unnecessarily".
In particular, if you do

iter2 = itertools.tee(iter1, 2)[0]

i.e. you "leak" one of the returned iterators, then all returned
objects are not collected until also iter2 is collected.

I propose a different implementation, namely the one in:

https://github.com/stephanh42/streamtee

streamtee.tee is a drop-in alternative for itertools.tee but
as you can see from the test in the repo, it will not hold
on to the generated objects as long.

I propose this as an improved implementation of itertools.tee.

Thanks,

Stephan


More information about the Python-ideas mailing list