Question
I'm having a hard time wrapping my brain around PEP 380.
How-To
Let's get one thing out of the way first. The explanation that yield from g is equivalent to for v in g: yield v does not even begin to do justice to what yield from is all about. Because, let's face it, if all yield from does is expand the for loop, then it does not warrant adding yield from to the language and preclude a whole bunch of new features from being implemented in Python 2.x.
What yield from does is it establishes a transparent bidirectional connection between the caller and the sub-generator:
BTW, if you are not sure what sending data to a generator even means, you need to drop everything and read about coroutines first—they're very useful (contrast them with subroutines), but unfortunately lesser-known in Python. Dave Beazley's Curious Course on Coroutines is an excellent start. Read slides 24-33 for a quick primer.
Reading data from a generator using yield from
- def reader():
- """A generator that fakes a read from a file, socket, etc."""
- for i in range(4):
- yield '<< %s' % i
- def reader_wrapper(g):
- # Manually iterate over data produced by reader
- for v in g:
- yield v
- wrap = reader_wrapper(reader())
- for i in wrap:
- print(i)
Instead of manually iterating over reader(), we can just yield from it:
- def reader_wrapper(g):
- yield from g
Sending data to a generator (coroutine) using yield from - Part 1
Now let's do something more interesting. Let's create a coroutine called writer that accepts data sent to it and writes to a socket, fd, etc.
- def writer():
- """A coroutine that writes data *sent* to it to fd, socket, etc."""
- while True:
- w = (yield)
- print('>> ', w)
- def writer_wrapper(coro):
- # TBD
- pass
- w = writer()
- wrap = writer_wrapper(w)
- wrap.send(None) # "prime" the coroutine
- for i in range(4):
- wrap.send(i)
The wrapper needs to accept the data that is sent to it (obviously) and should also handle the StopIteration when the for loop is exhausted. Evidently just doing for x in coro: yield x won't do. Here is a version that works:
- def writer_wrapper(coro):
- coro.send(None) # prime the coro
- while True:
- try:
- x = (yield) # Capture the value that's sent
- coro.send(x) # and pass it to the writer
- except StopIteration:
- pass
- def writer_wrapper(coro):
- yield from coro
Sending data to a generator yield from - Part 2 - Exception handling
Let's make it more complicated. What if our writer needs to handle exceptions? Let's say the writer handles a SpamException and it prints *** if it encounters one:
- class SpamException(Exception):
- pass
- def writer():
- while True:
- try:
- w = (yield)
- except SpamException:
- print('***')
- else:
- print('>> ', w)
- # writer_wrapper same as above
- w = writer()
- wrap = writer_wrapper(w)
- wrap.send(None) # "prime" the coroutine
- for i in [0, 1, 2, 'spam', 4]:
- if i == 'spam':
- wrap.throw(SpamException)
- else:
- wrap.send(i)
Actual Result
- >> 0
- >> 1
- >> 2
- Traceback (most recent call last):
- ... redacted ...
- File ... in writer_wrapper
- x = (yield)
- __main__.SpamException
- def writer_wrapper(coro):
- """Works. Manually catches exceptions and throws them"""
- coro.send(None) # prime the coro
- while True:
- try:
- try:
- x = (yield)
- except Exception as e: # This catches the SpamException
- coro.throw(e)
- else:
- coro.send(x)
- except StopIteration:
- pass
But so does this!
- def writer_wrapper(coro):
- yield from coro
This still does not cover all the corner cases though. What happens if the outer generator is closed? What about the case when the sub-generator returns a value (yes, in Python 3.3+, generators can return values), how should the return value be propagated? That yield from transparently handles all the corner cases is really impressive. yield from just magically works and handles all those cases.
In summary, it's best to think of yield from as a transparent two way channel between the caller and the sub-generator. References:
Supplement
* Python3: 淺談 Python 3.3 的 Yield From 表達式