【发布时间】:2016-05-08 06:24:27
【问题描述】:
我有一个带有return value 语句的生成器。
如果我在它上面使用下一个,我会得到 Stopiteration: value 从它按预期。
但是,当我使用 yield from 时,value 会丢失。
In [1]: def test():
...: return 1
...: yield 2
...:
In [2]: t = test()
In [3]: t
Out[3]: <generator object test at 0x000000000468F780>
In [4]: next(t)
---------------------------------------------------------------------------
StopIteration Traceback (most recent call last)
<ipython-input-4-9494367a8bed> in <module>()
----> 1 next(t)
StopIteration: 1
In [5]: def new():
...: yield from test()
...:
In [6]: n = new()
In [7]: n
Out[7]: <generator object new at 0x00000000050F23B8>
In [8]: next(n)
---------------------------------------------------------------------------
StopIteration Traceback (most recent call last)
<ipython-input-8-1c47c7af397e> in <module>()
----> 1 next(n)
StopIteration:
有没有办法在使用 yield from 时保留 value ?
这是按预期工作还是可能是一个错误?
【问题讨论】:
标签: python python-3.x generator yield-from