In Python, Can I Lazily Generate Copies Of An Iterator Using Tee?
I'm trying to create an iterator which lazily creates (potentially infinitely many) copies of an iterator. Is this possible? I know I can create any fixed finite number of copies b
Solution 1:
This is only barely touched upon in a single example near the bottom of the Python 2 itertools
documentation, but itertools.tee
supports copying:
import itertools, copy
definfinite_copies(some_iterable):
master, copy1 = itertools.tee(some_iterable)
yield copy1
whileTrue:
yield copy.copy(master)
The example in the documentation actually uses the __copy__
magic method, which is the hook used to customize copy.copy
behavior. (Apparently tee.__copy__
was added as part of a copyable iterators project that didn't go anywhere.)
Note that this will require storing every element ever produced by the original iterator, which can get very expensive. There is no way to avoid this cost.
Post a Comment for "In Python, Can I Lazily Generate Copies Of An Iterator Using Tee?"