Why Does Python's Itertools.cycle Need To Create A Copy Of The Iterable?
The documentation for Python's itertools.cycle() gives a pseudo-code implementation as: def cycle(iterable): # cycle('ABCD') --> A B C D A B C D A B C D ... saved = []
Solution 1:
Iterables can only be iterated over once.
You create a new iterable in your loop instead. Cycle cannot do that, it has to work with whatever you passed in. cycle
cannot simply recreate the iterable. It thus is forced to store all the elements the original iterator produces.
If you were to pass in the following generator instead, your loop()
fails:
deffinite_generator(source=[3, 2, 1]):
while source:
yield source.pop()
Now your loop()
produces:
>>>hard_limit = 6>>>for i in loop(finite_generator()):...if hard_limit <= 0:...break...print i... hard_limit -= 1...
1
2
3
Your code would only work for sequences, for which using cycle()
would be overkill; you don't need the storage burden of cycle()
in that case. Simplify it down to:
defloop_sequence(seq):
whileTrue:
for elem in seq:
yield elem
Post a Comment for "Why Does Python's Itertools.cycle Need To Create A Copy Of The Iterable?"