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 g2
the first time consumes it, so there's nothing left when you try to iterate over it subsequent times.
g1 = mygen()
for i in g1:
g2 = mygen()
for j in g2:
print i, j
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With