scdmb
scdmb

Reputation: 15641

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?

Upvotes: 102

Views: 64721

Answers (2)

user395760
user395760

Reputation:

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.

Upvotes: 122

Martijn Pieters
Martijn Pieters

Reputation: 1124748

The return value is not ignored, but generators only yield values, a return just ends the generator, in this case early. Advancing the generator never reaches the yield statement in that case.

Whenever a iterator reaches the 'end' of the values to yield, a StopIteration must be raised. Generators are no exception. As of Python 3.3 however, any return expression becomes the value of the exception:

>>> def gen():
...     return 3
...     yield 2
... 
>>> try:
...     next(gen())
... except StopIteration as ex:
...     e = ex
... 
>>> e
StopIteration(3,)
>>> e.value
3

Use the next() function to advance iterators, instead of calling .__next__() directly:

print(next(x))

Upvotes: 51

Related Questions