Say I’ve got a generator:
def mygen():
for i in range(10):
yield i
This works as I would expect: all combinations of i and j
for i in mygen():
for j in mygen():
print i, j
I would think these are different instances. Why are they not acting as different instances?
g1 = mygen()
g2 = mygen()
for i in g1:
for j in g2:
print i, j
If I try g1.next(), I get an error because there is no data left.
I’m running Python 2.7.1.
Iterating over
g2the first time consumes it, so there’s nothing left when you try to iterate over it subsequent times.