您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python SyntaxError :(“在生成器内部使用参数返回”,”)

Python SyntaxError :(“在生成器内部使用参数返回”,”)

您不能return在Python 2或Python 3.0-3.2中使用值来退出生成器。您需要使用 带表达式的yield加号:return __

if response.error:
    self.error("Error while retrieving the status")
    self.finish()
    yield error
    return

在循环本身中,yield再次使用:

for line in response.body.split("\n"):
    if line != "": 
        #net = int(line.split(" ")[1])
        #sens = int(line.split(" ")[2])
        #stype = int(line.split(" ")[3])
        value = int(line.split(" ")[4])
        print value
        yield value
        return

替代方法是引发异常或使用龙卷风回调。

在Python 3.3和更高版本中,return生成函数中带有值会导致该值附加到StopIterator异常中。对于async def异步生成器(Python 3.6及更高版本),return必须仍然没有价值。

python 2022/1/1 18:29:30 有612人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶