Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7d2fad1

Browse files
committed
Merge from 3.3: link to "yield from" examples in yield documentation.
2 parents ed3115b + 2654b86 commit 7d2fad1

2 files changed

Lines changed: 22 additions & 8 deletions

File tree

Doc/reference/expressions.rst

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,8 @@ Yield expressions
320320
yield_atom: "(" `yield_expression` ")"
321321
yield_expression: "yield" [`expression_list` | "from" `expression`]
322322

323-
The :keyword:`yield` expression is only used when defining a generator function,
323+
The :keyword:`yield` expression is only used when defining a :term:`generator`
324+
function,
324325
and can only be used in the body of a function definition. Using a
325326
:keyword:`yield` expression in a function definition is sufficient to cause that
326327
definition to create a generator function instead of a normal function.
@@ -438,6 +439,12 @@ is already executing raises a :exc:`ValueError` exception.
438439
other exception, it is propagated to the caller. :meth:`close` does nothing
439440
if the generator has already exited due to an exception or normal exit.
440441

442+
443+
.. index:: single: yield; examples
444+
445+
Examples
446+
^^^^^^^^
447+
441448
Here is a simple example that demonstrates the behavior of generators and
442449
generator functions::
443450

@@ -465,6 +472,9 @@ generator functions::
465472
>>> generator.close()
466473
Don't forget to clean up when 'close()' is called.
467474

475+
For examples using ``yield from``, see :ref:`pep-380` in "What's New in
476+
Python."
477+
468478

469479
.. seealso::
470480

Doc/whatsnew/3.3.rst

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

398401
PEP 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
402406
part 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.
404408
Additionally, the subgenerator is allowed to return with a value, and the
405409
value is made available to the delegating generator.
406410

@@ -421,23 +425,23 @@ However, unlike an ordinary loop, ``yield from`` allows subgenerators to
421425
receive sent and thrown values directly from the calling scope, and
422426
return 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

451455
The main principle driving this change is to allow even generators that are
452456
designed to be used with the ``send`` and ``throw`` methods to be split into

0 commit comments

Comments
 (0)