Python SyntaxError: ("'return' with argument inside generator",)

sharkbait picture sharkbait · Apr 4, 2013 · Viewed 37.7k times · Source

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("\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

I know that

for line in response.body.split

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?

Answer

Martijn Pieters picture Martijn Pieters · Apr 4, 2013

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

In the loop itself, use yield again:

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

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

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.