Home > Backend Development > Python Tutorial > Unlocking the Power of Bi-Directional Communication: What does \'yield from\' bring to Python 3.3?

Unlocking the Power of Bi-Directional Communication: What does \'yield from\' bring to Python 3.3?

DDD
Release: 2024-10-24 04:00:02
Original
748 people have browsed it

Unlocking the Power of Bi-Directional Communication: What does

The Power of "yield from" in Python 3.3

Python 3.3 introduced the "yield from" syntax, bringing a revolutionary approach to programming with generators and coroutines. While similar to regular generators, yield from establishes a direct connection between the caller and sub-generator, allowing for seamless bidirectional communication.

1. Reading Data from a Generator

The most straightforward use case is for reading data from a generator. By using yield from, we can receive values from the generator with a simplified syntax, similar to a regular loop.

For example, consider a generator that simulates reading data:

<code class="python">def reader():
    for i in range(4):
        yield '<< %s' % i
Copy after login

Using yield from, we can iterate over the data as follows:

<code class="python">def reader_wrapper(g):
    yield from g

wrap = reader_wrapper(reader())
for i in wrap:
    print(i)
Copy after login

2. Sending Data to a Coroutine

yield from shines when sending data to a coroutine. With yield from, data sent to the wrapper is transparently forwarded to the coroutine.

Consider a coroutine that writes data to a specific destination:

<code class="python">def writer():
    while True:
        w = (yield)
        print('>> ', w)</code>
Copy after login

To send data to this writer using yield from:

<code class="python">def writer_wrapper(coro):
    coro.send(None)
    while True:
        try:
            x = (yield)
            coro.send(x)
        except StopIteration:
            pass

w = writer()
wrap = writer_wrapper(w)
wrap.send(None)
for i in range(4):
    wrap.send(i)</code>
Copy after login

3. Exception Handling

yield from handles exceptions seamlessly. Exceptions raised in the sub-generator are propagated to the caller. Additionally, an exception sent to the wrapper can be thrown into the sub-generator.

For example, in our writer coroutine:

<code class="python">def writer():
    while True:
        try:
            w = (yield)
        except SpamException:
            print('***')
        else:
            print('>> ', w)</code>
Copy after login

Using yield from, we can handle exceptions as follows:

<code class="python">def writer_wrapper(coro):
    yield from coro</code>
Copy after login

4. The Power of Bidirectional Communication

The key aspect of yield from is the bidirectional connection it establishes. It allows for not only sending data to the coroutine but also propagates exceptions and returns the final value of the sub-generator.

Conclusion

yield from is a powerful tool that revolutionized generators and coroutines in Python. It simplifies bidirectional communication, handling exceptions seamlessly, and allowing for elegant solutions. Its use cases extend far beyond the examples discussed, making it an essential component of many advanced programming tasks in Python.

The above is the detailed content of Unlocking the Power of Bi-Directional Communication: What does \'yield from\' bring to Python 3.3?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template