"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How Do Return and Yield Work Together in Python 3.3 Generators?

How Do Return and Yield Work Together in Python 3.3 Generators?

Published on 2024-11-01
Browse:417

How Do Return and Yield Work Together in Python 3.3 Generators?

Using Return and Yield Together in Python Generators

In Python 2, a return statement inside a generator function that also used yield would result in an error. However, in Python 3.3, a subtle change occurred.

Code Demonstration

Consider the following Python 3.3 code:

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

Explanation

In this code, the function f includes both a return statement and a yield statement. When the function is called, the return statement is executed first, and the value 3 is returned. As a result, the yield statement is not executed.

When the generator x is iterated by calling its next method, a StopIteration exception is raised with a value of 3. This means that the generator's iterator is exhausted, and the value returned by the return statement is available as the value attribute of the exception.

New Mechanism in Python 3.3

According to PEP 380, this behavior is a new feature introduced in Python 3.3. It is equivalent to writing:

def f():
  yield 3
  raise StopIteration

Example with Yield from

The following example demonstrates how this behavior affects generators delegated using the yield from syntax:

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

In this case, the yield from statement delegates to the generator f. Since f returns a value but has no more yield statements, the value 1 is printed. However, the yield 2 statement in f is not executed.

Release Statement This article is reprinted at: 1729747611 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3