我正在帮助维护一些代码,这些代码现在包括自动 Python 3.7 测试。这让我想到了与 PEP 479 相关的一些问题“更改生成器内部的 StopIteration 处理”。我天真的理解是,您可以使用 try-except block 来修改旧代码以与所有 python 版本兼容,例如
旧代码:
def f1():
it = iter([0])
while True:
yield next(it)
print(list(f1()))
# [0] (in Py 3.6)
# "RuntimeError: generator raised StopIteration" (in Py 3.7;
# or using from __future__ import generator_stop)
变成:
def f2():
it = iter([0])
while True:
try:
yield next(it)
except StopIteration:
return
print(list(f2()))
# [0] (in all Python versions)
对于这个简单的示例,它可以工作,但我发现对于一些更复杂的代码,我正在重构它却没有。这是 Py 3.6 的最小示例:
class A(list):
it = iter([0])
def __init__(self):
while True:
self.append(next(self.it))
class B(list):
it = iter([0])
def __init__(self):
while True:
try:
self.append(next(self.it))
except StopIteration:
raise
class C(list):
it = iter([0])
def __init__(self):
while True:
try:
self.append(next(self.it))
except StopIteration:
return # or 'break'
def wrapper(MyClass):
lst = MyClass()
for item in lst:
yield item
print(list(wrapper(A)))
# [] (wrong output)
print(list(wrapper(B)))
# [] (wrong output)
print(list(wrapper(C)))
# [0] (desired output)
我知道 A
和 B
示例完全相同,C
示例是与 Python 3.7 兼容的正确方式(我还知道重构为 for
循环对许多示例都有意义,包括这个人为设计的示例)。
但问题是为什么带有 A
和 B
的示例会生成一个空列表 []
,而不是 [0 ]
?
最佳答案
前两种情况在类的 __init__
中引发了未捕获的 StopIteration
。 list
构造函数在 Python 3.6 中处理得很好(可能有警告,具体取决于版本)。但是,异常传播 before wrapper
有机会迭代:有效失败的行是 lst = MyClass()
,循环 for item in lst:
从不运行,导致生成器为空。
当我在 Python 3.6.4 中运行此代码时,我在 print
行(对于 A
和 B
)都收到以下警告:
DeprecationWarning: generator 'wrapper' raised StopIteration
这里的结论是双重的:
- 不要让迭代器自行耗尽。检查它何时停止是你的工作。使用
for
循环很容易做到这一点,但必须使用while
循环手动完成。案例A
就是一个很好的例证。 - 不要重新引发内部异常。而是返回
None
。 CaseB
不是可行的方法。break
或return
将在except
block 中正常工作,就像您在C
中所做的那样。
鉴于 for
循环是 C
中 try-except block 的语法糖,我通常会推荐使用它们,即使手动调用 iter
:
class D(list):
it = iter([0])
def __init__(self):
for item in it:
self.append(item)
此版本在功能上等同于 C
,并为您完成所有簿记工作。很少有情况需要实际的 while
循环(跳过对 next
的调用是我想到的一种情况,但即使是这些情况也可以用嵌套循环重写)。
关于python - 了解非平凡情况下生成器内部的 StopIteration 处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53046504/