<tfoot id='d2DRD'></tfoot>
      <i id='d2DRD'><tr id='d2DRD'><dt id='d2DRD'><q id='d2DRD'><span id='d2DRD'><b id='d2DRD'><form id='d2DRD'><ins id='d2DRD'></ins><ul id='d2DRD'></ul><sub id='d2DRD'></sub></form><legend id='d2DRD'></legend><bdo id='d2DRD'><pre id='d2DRD'><center id='d2DRD'></center></pre></bdo></b><th id='d2DRD'></th></span></q></dt></tr></i><div id='d2DRD'><tfoot id='d2DRD'></tfoot><dl id='d2DRD'><fieldset id='d2DRD'></fieldset></dl></div>

        <bdo id='d2DRD'></bdo><ul id='d2DRD'></ul>
    1. <small id='d2DRD'></small><noframes id='d2DRD'>

      <legend id='d2DRD'><style id='d2DRD'><dir id='d2DRD'><q id='d2DRD'></q></dir></style></legend>

      Python SyntaxError: ("'return' with argument in

      时间:2023-09-02
      <legend id='YWDr9'><style id='YWDr9'><dir id='YWDr9'><q id='YWDr9'></q></dir></style></legend>

    2. <small id='YWDr9'></small><noframes id='YWDr9'>

        <bdo id='YWDr9'></bdo><ul id='YWDr9'></ul>
          <tfoot id='YWDr9'></tfoot>
              <tbody id='YWDr9'></tbody>

              1. <i id='YWDr9'><tr id='YWDr9'><dt id='YWDr9'><q id='YWDr9'><span id='YWDr9'><b id='YWDr9'><form id='YWDr9'><ins id='YWDr9'></ins><ul id='YWDr9'></ul><sub id='YWDr9'></sub></form><legend id='YWDr9'></legend><bdo id='YWDr9'><pre id='YWDr9'><center id='YWDr9'></center></pre></bdo></b><th id='YWDr9'></th></span></q></dt></tr></i><div id='YWDr9'><tfoot id='YWDr9'></tfoot><dl id='YWDr9'><fieldset id='YWDr9'></fieldset></dl></div>
              2. 本文介绍了Python SyntaxError: ("'return' with argument inside generator",)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我的 Python 程序中有这个函数:

                I have this function in my Python program:

                @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("
                "):
                                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
                

                是一个生成器.但我会将 value 变量返回给调用该函数的处理程序.这可能吗?我该怎么办?

                is a generator. But I would return the value variable to the handler that called the function. It's this possible? How can I do?

                推荐答案

                在 Python 2 或 Python 3.0 - 3.2 中,您不能使用带有值的 return 来退出生成器.你需要使用 yield 加上一个 return without 一个表达式:

                You cannot use return with a value to exit a generator in Python 2, or Python 3.0 - 3.2. You need to use yield plus a return without an expression:

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

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

                In the loop itself, use yield again:

                for line in response.body.split("
                "):
                    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
                

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

                Alternatives are to raise an exception or to use tornado callbacks instead.

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

                In Python 3.3 and newer, return with a value in a generator function results in the value being attached to the StopIterator exception. For async def asynchronous generators (Python 3.6 and up), return must still be value-less.

                这篇关于Python SyntaxError: ("'return' with argument inside generator",)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:'Return' 关键字只返回循环中的一个元素? 下一篇:“回归"在函数中只返回一个值

                相关文章

                    <bdo id='cs0Kb'></bdo><ul id='cs0Kb'></ul>

                  <small id='cs0Kb'></small><noframes id='cs0Kb'>

                  <tfoot id='cs0Kb'></tfoot>

                  <i id='cs0Kb'><tr id='cs0Kb'><dt id='cs0Kb'><q id='cs0Kb'><span id='cs0Kb'><b id='cs0Kb'><form id='cs0Kb'><ins id='cs0Kb'></ins><ul id='cs0Kb'></ul><sub id='cs0Kb'></sub></form><legend id='cs0Kb'></legend><bdo id='cs0Kb'><pre id='cs0Kb'><center id='cs0Kb'></center></pre></bdo></b><th id='cs0Kb'></th></span></q></dt></tr></i><div id='cs0Kb'><tfoot id='cs0Kb'></tfoot><dl id='cs0Kb'><fieldset id='cs0Kb'></fieldset></dl></div>

                  1. <legend id='cs0Kb'><style id='cs0Kb'><dir id='cs0Kb'><q id='cs0Kb'></q></dir></style></legend>