I have a class that contains an instance itertools.cyclethat I would like to copy. One approach (the only one I can think of) is to extract the original iterable (which was a list) and keep the position the loop is in.
Unfortunately, I cannot get the list that I used to create the loop instance, and there is no obvious way to do this:
import itertools
c = itertools.cycle([1, 2, 3])
print dir(c)
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__',
'__hash__', '__init__', '__iter__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__',
'__subclasshook__', 'next']
I can find some reasonable reasons why this would be forbidden for some types of input iterations, but for a tuple or maybe even a list (maybe volatility could be a problem), I don’t understand why this would be possible.
Does anyone know if it can be extracted from an infinite iterable from an instance itertools.cycle. If not, does anyone know why this idea is bad?
source
share