@@ -393,14 +393,18 @@ inspection of exception attributes::
393393 PEP written and implemented by Antoine Pitrou
394394
395395
396+ .. index ::
397+ single: yield; yield from (in What's New)
398+
396399.. _pep-380 :
397400
398401PEP 380: Syntax for Delegating to a Subgenerator
399402================================================
400403
401- PEP 380 adds the ``yield from `` expression, allowing a generator to delegate
404+ PEP 380 adds the ``yield from `` expression, allowing a :term: `generator ` to
405+ delegate
402406part of its operations to another generator. This allows a section of code
403- containing ' yield' to be factored out and placed in another generator.
407+ containing :keyword: ` yield ` to be factored out and placed in another generator.
404408Additionally, the subgenerator is allowed to return with a value, and the
405409value is made available to the delegating generator.
406410
@@ -421,23 +425,23 @@ However, unlike an ordinary loop, ``yield from`` allows subgenerators to
421425receive sent and thrown values directly from the calling scope, and
422426return a final value to the outer generator::
423427
424- >>> def accumulate(start=0 ):
425- ... tally = start
428+ >>> def accumulate():
429+ ... tally = 0
426430 ... while 1:
427431 ... next = yield
428432 ... if next is None:
429433 ... return tally
430434 ... tally += next
431435 ...
432- >>> def gather_tallies(tallies, start=0 ):
436+ >>> def gather_tallies(tallies):
433437 ... while 1:
434438 ... tally = yield from accumulate()
435439 ... tallies.append(tally)
436440 ...
437441 >>> tallies = []
438442 >>> acc = gather_tallies(tallies)
439443 >>> next(acc) # Ensure the accumulator is ready to accept values
440- >>> for i in range(10 ):
444+ >>> for i in range(4 ):
441445 ... acc.send(i)
442446 ...
443447 >>> acc.send(None) # Finish the first tally
@@ -446,7 +450,7 @@ return a final value to the outer generator::
446450 ...
447451 >>> acc.send(None) # Finish the second tally
448452 >>> tallies
449- [45 , 10]
453+ [6 , 10]
450454
451455The main principle driving this change is to allow even generators that are
452456designed to be used with the ``send `` and ``throw `` methods to be split into
0 commit comments