Search code examples
pythongeneratorpython-3.3

Return in generator together with yield


In Python 2 it used to cause an error when return occurred together with yield inside a function definition. But for this code in Python 3.3:

def f():
  return 3
  yield 2
  
x = f()
print(x.__next__())

there is no error that return is used in function with yield. However when the function __next__ is called then there is thrown exception StopIteration. Why there is not just returned value 3? Is this return somehow ignored?


Solution

  • This is a new feature in Python 3.3. Much like return in a generator has long been equivalent to raise StopIteration(), return <something> in a generator is now equivalent to raise StopIteration(<something>). For that reason, the exception you're seeing should be printed as StopIteration: 3, and the value is accessible through the attribute value on the exception object. If the generator is delegated to using the (also new) yield from syntax, it is the result. See PEP 380 for details.

    def f():
        return 1
        yield 2
    
    def g():
        x = yield from f()
        print(x)
    
    # g is still a generator so we need to iterate to run it:
    for _ in g():
        pass
    

    This prints 1, but not 2.