Trouble understanding python generators
This question already has an answer here:
Generators (like all iterables) can only be iterated over once. By the time print_gen(x)
is done, so is x
. Any further attempts to get new values from x
will result in StopIteration
being raised.
This works:
x = do_gen()
y = (incr_gen(i) for i in do_gen())
print_gen(x)
print_gen(y)
as that creates two separate independent generators. In your version, the generator expression you assigned to y
expects x
to yield more values.
It is easier to see that the x
generator is shared with y
when you use the next()
function on them in turn:
>>> def do_gen():
... for i in range(3):
... yield i
...
>>> def incr_gen(y):
... return y + 1
...
>>> x = do_gen()
>>> y = (incr_gen(i) for i in x)
>>> next(x) # first value for the range
0
>>> next(y) # second value from the range, plus 1
2
>>> next(x) # third value from the range
2
>>> next(y) # no more values available, generator done
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
Note the StopIteration
raised by next(y)
as well here.
上一篇: “对于我在发电机()中:”做什么?
下一篇: 无法理解python生成器