-->

Python的语法错误:(“‘回归’里面生成参数”)(Python SyntaxError: (“&

2019-08-19 11:17发布

我有我的Python程序这个功能:

@tornado.gen.engine
def check_status_changes(netid, sensid):        
    como_url = "".join(['http://131.114.52:44444/ztc?netid=', str(netid), '&sensid=', str(sensid), '&start=-5s&end=-1s'])

    http_client = AsyncHTTPClient()
    response = yield tornado.gen.Task(http_client.fetch, como_url)

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

    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
                    return value

我知道

for line in response.body.split

是发电机。 不过,我想变量的值返回到调用该函数的处理程序。 这是这可能吗? 我能怎么做?

Answer 1:

您不能使用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 SyntaxError: (“'return' with argument inside generator”,)