首页 > 解决方案 > 了解生成器内部的 StopIteration 处理非平凡情况

问题描述

我正在帮助维护一些现在包含自动化 Python 3.7 测试的代码。这导致我遇到了一些与PEP 479 “更改生成器内部的停止迭代处理”相关的问题。我天真的理解是,您可以使用 try-except 块来修改旧代码以与所有 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)

我知道AandB示例是完全等价的,并且Ccase 是与 Python 3.7 兼容的正确方式(我也知道重构为for循环对于许多示例都是有意义的,包括这个人为的示例)。

但问题是为什么这些例子AB产生一个空列表[],而不是[0]

标签: pythonpython-3.xexceptiongeneratorstopiteration

解决方案


前两个案例StopIteration在课堂上提出了一个未被发现的问题__init__。构造list函数在 Python 3.6 中处理得很好(可能会出现警告,具体取决于版本)。但是,异常在有机会迭代之前 wrapper传播:有效失败的行是lst = MyClass(),并且循环for item in lst:永远不会运行,导致生成器为空。

print当我在 Python 3.6.4 中运行此代码时,我在两行(forA和)上都收到以下警告B

DeprecationWarning: generator 'wrapper' raised StopIteration

这里的结论是双重的:

  1. 不要让迭代器自行耗尽。检查它何时停止是你的工作。使用循环很容易做到这一点for,但必须使用while循环手动完成。案例A是一个很好的例证。
  2. 不要重新引发内部异常。None改为返回。案例B不是要走的路。A breakorreturn将在except块中正常工作,就像您在C.

鉴于for循环是 try-except 块中的语法糖C,我通常建议使用它们,即使手动调用iter

class D(list):
    it = iter([0])
    def __init__(self):
        for item in it:
            self.append(item)

此版本在功能上等同于C,并为您完成所有簿记工作。很少有需要实际while循环的情况(跳过调用next成为想到的一种,但即使是这些情况也可以用嵌套循环重写)。


推荐阅读