在 Python 3.3 中与生成器一起返回生成器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16780002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:39:40  来源:igfitidea点击:

Return in generator together with yield in Python 3.3

pythonpython-3.xgenerator

提问by scdmb

In Python 2 there was an error when return was together with yield in function definition. But for this code in Python 3.3

在 Python 2 中,函数定义中 return 与 yield 一起出现错误。但是对于 Python 3.3 中的这段代码

def f():
  return 3
  yield 2

x = f()
print(x.__next__())

there is no error that return is used in function with yield. However when the function __next__is called then there is thrown exception StopIteration. Why there is not just returned value 3? Is this return somehow ignored?

没有错误在函数中使用 return 和 yield。但是,当__next__调用该函数时,会抛出异常 StopIteration。为什么不只是返回值3?这个回报以某种方式被忽略了吗?

采纳答案by Martijn Pieters

This is a new feature in Python 3.3 (as a comment notes, it doesn't even work in 3.2). Much like returnin a generator has long been equivalent to raise StopIteration(), return <something>in a generator is now equivalent to raise StopIteration(<something>). For that reason, the exception you're seeing should be printed as StopIteration: 3, and the value is accessible through the attribute valueon the exception object. If the generator is delegated to using the (also new) yield fromsyntax, it is the result. See PEP 380for details.

这是 Python 3.3 中的一个新功能(作为注释,它甚至在 3.2 中都不起作用)。很像return在生成器中早已等价于raise StopIteration()return <something>在生成器中现在等价于raise StopIteration(<something>)。因此,您看到的异常应该打印为StopIteration: 3,并且可以通过value异常对象上的属性访问该值。如果生成器被委托使用(也是新的)yield from语法,它就是结果。有关详细信息,请参阅PEP 380

def f():
    return 1
    yield 2

def g():
    x = yield from f()
    print(x)

# g is still a generator so we need to iterate to run it:
for _ in g():
    pass

This prints 1, but not 2.

这会打印1,但不会2

回答by Martijn Pieters

The return value is not ignored, but generators only yieldvalues, a returnjust ends the generator, in this case early. Advancing the generator never reaches the yieldstatement in that case.

返回值不会被忽略,但生成器只产生值,areturn只是结束生成器,在这种情况下提前。yield在这种情况下,推进生成器永远不会到达语句。

Whenever a iterator reaches the 'end' of the values to yield, a StopIterationmustbe raised. Generators are no exception. As of Python 3.3 however, any returnexpression becomes the value of the exception:

每当迭代器到达要产生的值的“结尾”时,StopIteration就必须提高 a。发电机也不例外。然而,从 Python 3.3 开始,任何return表达式都会成为异常的值:

>>> def gen():
...     return 3
...     yield 2
... 
>>> try:
...     next(gen())
... except StopIteration as ex:
...     e = ex
... 
>>> e
StopIteration(3,)
>>> e.value
3

Use the next()function to advance iterators, instead of calling .__next__()directly:

使用next()函数推进迭代器,而不是.__next__()直接调用:

print(next(x))