从具有返回<value>语句的生成器中获取

时间:2016-01-30 14:11:57

标签: python python-3.x generator yield-from

我有一个带有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:

使用value时,有没有办法保留yield from? 这是按预期工作还是可能是一个错误?

1 个答案:

答案 0 :(得分:5)

通过在yield from语句中接收子生成器发送的值。

引用 PEP 380 -- Syntax for Delegating to a Subgenerator:

的引用
  

yield from表达式的值是迭代器终止时引发的StopIteration异常的第一个参数。

通过小幅调整,res生成器中的new将包含从StopIteration子生成器中提升的test值:

def new():
   res = yield from test()
   return res

现在执行next(n)时,您将获得“异常”消息中的值:

n = new()

next(n)
---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
<ipython-input-39-1c47c7af397e> in <module>()
----> 1 next(n)

StopIteration: 1

哦,作为附录,您当然可以获得“回归”。通过再次使用StopIteration将值封装在yield对象中的值:

def new():
    res = yield from test()
    yield res

现在调用next(new())将返回test()返回的值:

next(new())
Out[20]: 1
相关问题