Return in generator together with yield in Python 3.3(在 Python 3.3 中与生成器一起返回)
问题描述
在 Python 2 中,函数定义中 return 与 yield 一起出现错误.但是对于 Python 3.3 中的这段代码
In Python 2 there was an error when return was together with yield in function definition. But for this code in Python 3.3
def f():
return 3
yield 2
x = f()
print(x.__next__())
在有yield的函数中使用return没有错误.但是,当调用函数 __next__ 时,会抛出异常 StopIteration.为什么没有返回值3?这个返回值是否被忽略了?
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?
推荐答案
这是 Python 3.3 中的一个新特性(正如注释所说,它甚至在 3.2 中都不起作用).就像生成器中的 return 长期以来等价于 raise StopIteration() 一样,生成器中的 return <something> 现在等价于 <代码>引发 StopIteration(<something>).因此,您看到的异常应该打印为 StopIteration: 3,并且可以通过异常对象上的属性 value 访问该值.如果生成器被委托使用(也是新的)yield from 语法,它就是结果.有关详细信息,请参阅 PEP 380.
This is a new feature in Python 3.3 (as a comment notes, it doesn't even work in 3.2). Much like return in 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 value on the exception object. If the generator is delegated to using the (also new) yield from syntax, it is the result. See PEP 380 for details.
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
这会打印 1,但不会打印 2.
This prints 1, but not 2.
这篇关于在 Python 3.3 中与生成器一起返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Python 3.3 中与生成器一起返回
基础教程推荐
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
