生成器方法throw的返回值

问题描述

抛出的帮助说:

buildins.generator实例的

throw(...)方法 throw(typ [,val [,tb]])->在生成器中引发异常, 返回下一个产生的值或提高StopIteration

“返回下一个产生的值”,这是我的问题!

让我们看看这个例子:

def gen():
    counter = 1
    while True:
        try:
            yield counter
            counter += 1
        except Exception as e:
            print("Exception and tpye: ",e,type(e))
            

x = gen()
print("Result of first next call: ",next(x))
res = x.throw(ValueError("Whatever!"))
print("result of throw call: ",res)
print("Result of first next call: ",next(x))

输出看起来像这样:

Result of first next call:  1
Exception and tpye:  Whatever! <class 'ValueError'>
result of throw call:  1
Result of first next call:  2

throw的返回值不是2而是1?帮助文件显示“返回NEXT产生值2”而不是“再次返回先前产生的值”

恐怕我完全错了吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)