我正在阅读Python 3 here的文档:
如果生成器代码直接或间接引发
StopIteration
,则会将其转换为RuntimeError
(保留StopIteration
作为新的异常原因)。
我不明白,有人可以解释吗?
这是我在Python 3.6中尝试过的方法,但似乎没有发现任何问题:
def gen1():
yield from [1, 2, 3]
raise StopIteration
def gen2():
raise StopIteration
try:
a = list(gen1())
# a == [1, 2, 3]
except RuntimeError:
print("Caught")
try:
a = gen1()
next(a), next(a), next(a), next(a), next(a)
except RuntimeError:
print("Caught")
try:
gen2()
except RuntimeError:
print("Caught")
try:
a = list(gen2())
except RuntimeError:
print("Caught")
特别是,两个对gen2()
的调用都引发了StopIteration
,但仍未转换为RuntimeError
。
答案 0 :(得分:8)
您错过了此更改适用于Python 3.7及更高版本。 除非您先启用from __future__
导入功能(自Python 3.5起可用),否则您将不会在Python 3.6或更早版本中看到转换。
在您链接的同一页面上:
在版本3.5中进行了更改:通过
RuntimeError
引入了from __future__ import generator_stop
转换,请参阅PEP 479。版本3.7中的更改:默认情况下为所有代码启用PEP 479:生成器中引发的
StopIteration
错误将转换为RuntimeError
。
PEP 479 -- Change StopIteration handling inside generators进一步详细说明了进行此更改的原因及其应用方式。对于在Python 3.7上运行的代码,输出将变为:
>>> import sys
>>> sys.version_info
sys.version_info(major=3, minor=7, micro=0, releaselevel='final', serial=0)
>>> def gen1():
... yield from [1, 2, 3]
... raise StopIteration
...
>>> def gen2():
... yield 42 # make this an actual generator
... raise StopIteration
...
>>> try:
... a = list(gen1())
... except RuntimeError:
... print("Caught")
...
Caught
>>> try:
... a = gen1()
... next(a), next(a), next(a), next(a), next(a)
... except RuntimeError:
... print("Caught")
...
Caught
>>> try:
... a = list(gen2())
... except RuntimeError:
... print("Caught")
...
Caught
请注意,我在yield 42
上添加了gen2()
行以使其成为生成器。如果体内没有yield
或yield from
,则会得到一个常规函数。调用生成器函数将生成一个生成器对象,并且函数主体开始处于暂停状态,而调用普通函数将立即执行该主体:
>>> def normal():
... raise StopIteration
...
>>> def generator():
... raise StopIteration
... yield # never reached, but this is now a generator
...
>>> normal()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in normal
StopIteration
>>> generator()
<generator object generator at 0x105831ed0>
>>> next(generator())
Traceback (most recent call last):
File "<stdin>", line 2, in generator
StopIteration
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: generator raised StopIteration
对于Python 3.6,您可以使用from __future__ import generator_stop
编译器开关(在编写脚本或模块时在代码顶部使用它):
>>> import sys
>>> sys.version_info
sys.version_info(major=3, minor=6, micro=5, releaselevel='final', serial=0)
>>> def generator():
... raise StopIteration
... yield
...
>>> next(generator())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in generator
StopIteration
>>> from __future__ import generator_stop
>>> def generator(): # re-define it so it is compiled anew
... raise StopIteration
... yield
...
>>> next(generator())
Traceback (most recent call last):
File "<stdin>", line 2, in generator
StopIteration
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: generator raised StopIteration