2
我有一個發電機,其中有return value
聲明。 如果我使用它,我會按預期從它那裏得到Stopiteration:value
。 但是,當我使用yield from
value
丟失。從發電機產生的收益<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:
有沒有辦法保存value
使用時yield from
? 這是按預期工作,或者它可能是一個錯誤?