diff options
author | Chris Jerdonek <chris.jerdonek@gmail.com> | 2012-12-23 23:31:57 (GMT) |
---|---|---|
committer | Chris Jerdonek <chris.jerdonek@gmail.com> | 2012-12-23 23:31:57 (GMT) |
commit | 2654b86e88aa14f0916cfa10ecdbc0852e7e6795 (patch) | |
tree | 4dfe31ce3f21cea1854bea3db4c95f5ddd06d70d /Doc/whatsnew | |
parent | 70dcef478936c6df43bc6f812b52d58a0cd219b4 (diff) | |
download | cpython-2654b86e88aa14f0916cfa10ecdbc0852e7e6795.zip cpython-2654b86e88aa14f0916cfa10ecdbc0852e7e6795.tar.gz cpython-2654b86e88aa14f0916cfa10ecdbc0852e7e6795.tar.bz2 |
Link to "yield from" examples in yield documentation.
This commit also simplifies the more advanced "yield from" example and removes
unused function parameters.
Diffstat (limited to 'Doc/whatsnew')
-rw-r--r-- | Doc/whatsnew/3.3.rst | 18 |
1 files changed, 11 insertions, 7 deletions
diff --git a/Doc/whatsnew/3.3.rst b/Doc/whatsnew/3.3.rst index 4b9dc9c..4ea4995 100644 --- a/Doc/whatsnew/3.3.rst +++ b/Doc/whatsnew/3.3.rst @@ -393,14 +393,18 @@ inspection of exception attributes:: PEP written and implemented by Antoine Pitrou +.. index:: + single: yield; yield from (in What's New) + .. _pep-380: PEP 380: Syntax for Delegating to a Subgenerator ================================================ -PEP 380 adds the ``yield from`` expression, allowing a generator to delegate +PEP 380 adds the ``yield from`` expression, allowing a :term:`generator` to +delegate part of its operations to another generator. This allows a section of code -containing 'yield' to be factored out and placed in another generator. +containing :keyword:`yield` to be factored out and placed in another generator. Additionally, the subgenerator is allowed to return with a value, and the value is made available to the delegating generator. @@ -421,15 +425,15 @@ However, unlike an ordinary loop, ``yield from`` allows subgenerators to receive sent and thrown values directly from the calling scope, and return a final value to the outer generator:: - >>> def accumulate(start=0): - ... tally = start + >>> def accumulate(): + ... tally = 0 ... while 1: ... next = yield ... if next is None: ... return tally ... tally += next ... - >>> def gather_tallies(tallies, start=0): + >>> def gather_tallies(tallies): ... while 1: ... tally = yield from accumulate() ... tallies.append(tally) @@ -437,7 +441,7 @@ return a final value to the outer generator:: >>> tallies = [] >>> acc = gather_tallies(tallies) >>> next(acc) # Ensure the accumulator is ready to accept values - >>> for i in range(10): + >>> for i in range(4): ... acc.send(i) ... >>> acc.send(None) # Finish the first tally @@ -446,7 +450,7 @@ return a final value to the outer generator:: ... >>> acc.send(None) # Finish the second tally >>> tallies - [45, 10] + [6, 10] The main principle driving this change is to allow even generators that are designed to be used with the ``send`` and ``throw`` methods to be split into |