Let's say I have a large list of data that I want to perform some operation on, and I would like to have multiple iterators performing this operation independently.
data = [1,2,3,4,5]
generator = ((e, 2*e) for e in data)
it1 = iter(generator)
it2 = iter(generator)
I would expect these iterators to be different code objects, but it1 is it2 returns True... More confusingly, this is true for the following generators as well:
# copied data
gen = ((e, 2*e) for e in copy.deepcopy(data))
# temp object
gen = ((e, 2*e) for e in [1,2,3,4,5])
This means in practice that when I call next(it1), it2 is incremented as well, which is not the behavior I want.
What is going on here, and is there any way to do what I'm trying to do? I am using python 2.7 on Ubuntu 14.04.
Edit:
I just tried out the following as well:
gen = (e for e in [1,2,3,4,5])
it = iter(gen)
next(it)
next(it)
for e in gen:
print e
Which prints 3 4 5... Apparently generators are just a more constrained concept that I had imagined.