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

Skip to content

Commit d1c85fd

Browse files
committed
eliminate redundancy between yield stmt and yield expr docs (closes #12704)
Patch by Nikolaus Rath.
1 parent 91d4278 commit d1c85fd

2 files changed

Lines changed: 62 additions & 94 deletions

File tree

Doc/reference/expressions.rst

Lines changed: 47 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -319,27 +319,25 @@ Yield expressions
319319
yield_atom: "(" `yield_expression` ")"
320320
yield_expression: "yield" [`expression_list` | "from" `expression`]
321321

322-
The :keyword:`yield` expression is only used when defining a :term:`generator`
323-
function,
324-
and can only be used in the body of a function definition. Using a
325-
:keyword:`yield` expression in a function definition is sufficient to cause that
326-
definition to create a generator function instead of a normal function.
322+
The yield expression is only used when defining a :term:`generator` function and
323+
thus can only be used in the body of a function definition. Using a yield
324+
expression in a function's body causes that function to be a generator.
327325

328326
When a generator function is called, it returns an iterator known as a
329327
generator. That generator then controls the execution of a generator function.
330328
The execution starts when one of the generator's methods is called. At that
331-
time, the execution proceeds to the first :keyword:`yield` expression, where it
332-
is suspended again, returning the value of :token:`expression_list` to
333-
generator's caller. By suspended we mean that all local state is retained,
334-
including the current bindings of local variables, the instruction pointer, and
335-
the internal evaluation stack. When the execution is resumed by calling one of
336-
the generator's methods, the function can proceed exactly as if the
337-
:keyword:`yield` expression was just another external call. The value of the
338-
:keyword:`yield` expression after resuming depends on the method which resumed
339-
the execution. If :meth:`~generator.__next__` is used (typically via either a
340-
:keyword:`for` or the :func:`next` builtin) then the result is :const:`None`,
341-
otherwise, if :meth:`~generator.send` is used, then the result will be the
342-
value passed in to that method.
329+
time, the execution proceeds to the first yield expression, where it is
330+
suspended again, returning the value of :token:`expression_list` to generator's
331+
caller. By suspended, we mean that all local state is retained, including the
332+
current bindings of local variables, the instruction pointer, and the internal
333+
evaluation stack. When the execution is resumed by calling one of the
334+
generator's methods, the function can proceed exactly as if the yield expression
335+
was just another external call. The value of the yield expression after
336+
resuming depends on the method which resumed the execution. If
337+
:meth:`~generator.__next__` is used (typically via either a :keyword:`for` or
338+
the :func:`next` builtin) then the result is :const:`None`. Otherwise, if
339+
:meth:`~generator.send` is used, then the result will be the value passed in to
340+
that method.
343341

344342
.. index:: single: coroutine
345343

@@ -349,11 +347,11 @@ suspended. The only difference is that a generator function cannot control
349347
where should the execution continue after it yields; the control is always
350348
transferred to the generator's caller.
351349

352-
:keyword:`yield` expressions are allowed in the :keyword:`try` clause of a
353-
:keyword:`try` ... :keyword:`finally` construct. If the generator is not
354-
resumed before it is finalized (by reaching a zero reference count or by being
355-
garbage collected), the generator-iterator's :meth:`~generator.close` method
356-
will be called, allowing any pending :keyword:`finally` clauses to execute.
350+
yield expressions are allowed in the :keyword:`try` clause of a :keyword:`try`
351+
... :keyword:`finally` construct. If the generator is not resumed before it is
352+
finalized (by reaching a zero reference count or by being garbage collected),
353+
the generator-iterator's :meth:`~generator.close` method will be called,
354+
allowing any pending :keyword:`finally` clauses to execute.
357355

358356
When ``yield from <expr>`` is used, it treats the supplied expression as
359357
a subiterator. All values produced by that subiterator are passed directly
@@ -373,11 +371,23 @@ the yield expression. It can be either set explicitly when raising
373371
.. versionchanged:: 3.3
374372
Added ``yield from <expr>`` to delegate control flow to a subiterator
375373

376-
The parentheses can be omitted when the :keyword:`yield` expression is the
377-
sole expression on the right hand side of an assignment statement.
374+
The parentheses may be omitted when the yield expression is the sole expression
375+
on the right hand side of an assignment statement.
378376

379-
.. index:: object: generator
377+
.. seealso::
378+
379+
:pep:`0255` - Simple Generators
380+
The proposal for adding generators and the :keyword:`yield` statement to Python.
381+
382+
:pep:`0342` - Coroutines via Enhanced Generators
383+
The proposal to enhance the API and syntax of generators, making them
384+
usable as simple coroutines.
380385

386+
:pep:`0380` - Syntax for Delegating to a Subgenerator
387+
The proposal to introduce the :token:`yield_from` syntax, making delegation
388+
to sub-generators easy.
389+
390+
.. index:: object: generator
381391

382392
Generator-iterator methods
383393
^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -395,13 +405,12 @@ is already executing raises a :exc:`ValueError` exception.
395405
.. method:: generator.__next__()
396406

397407
Starts the execution of a generator function or resumes it at the last
398-
executed :keyword:`yield` expression. When a generator function is resumed
399-
with a :meth:`~generator.__next__` method, the current :keyword:`yield`
400-
expression always evaluates to :const:`None`. The execution then continues
401-
to the next :keyword:`yield` expression, where the generator is suspended
402-
again, and the value of the :token:`expression_list` is returned to
403-
:meth:`next`'s caller.
404-
If the generator exits without yielding another value, a :exc:`StopIteration`
408+
executed yield expression. When a generator function is resumed with a
409+
:meth:`~generator.__next__` method, the current yield expression always
410+
evaluates to :const:`None`. The execution then continues to the next yield
411+
expression, where the generator is suspended again, and the value of the
412+
:token:`expression_list` is returned to :meth:`next`'s caller. If the
413+
generator exits without yielding another value, a :exc:`StopIteration`
405414
exception is raised.
406415

407416
This method is normally called implicitly, e.g. by a :keyword:`for` loop, or
@@ -411,12 +420,12 @@ is already executing raises a :exc:`ValueError` exception.
411420
.. method:: generator.send(value)
412421

413422
Resumes the execution and "sends" a value into the generator function. The
414-
``value`` argument becomes the result of the current :keyword:`yield`
415-
expression. The :meth:`send` method returns the next value yielded by the
416-
generator, or raises :exc:`StopIteration` if the generator exits without
417-
yielding another value. When :meth:`send` is called to start the generator,
418-
it must be called with :const:`None` as the argument, because there is no
419-
:keyword:`yield` expression that could receive the value.
423+
*value* argument becomes the result of the current yield expression. The
424+
:meth:`send` method returns the next value yielded by the generator, or
425+
raises :exc:`StopIteration` if the generator exits without yielding another
426+
value. When :meth:`send` is called to start the generator, it must be called
427+
with :const:`None` as the argument, because there is no yield expression that
428+
could receive the value.
420429

421430

422431
.. method:: generator.throw(type[, value[, traceback]])
@@ -478,20 +487,6 @@ For examples using ``yield from``, see :ref:`pep-380` in "What's New in
478487
Python."
479488

480489

481-
.. seealso::
482-
483-
:pep:`0255` - Simple Generators
484-
The proposal for adding generators and the :keyword:`yield` statement to Python.
485-
486-
:pep:`0342` - Coroutines via Enhanced Generators
487-
The proposal to enhance the API and syntax of generators, making them
488-
usable as simple coroutines.
489-
490-
:pep:`0380` - Syntax for Delegating to a Subgenerator
491-
The proposal to introduce the :token:`yield_from` syntax, making delegation
492-
to sub-generators easy.
493-
494-
495490
.. _primaries:
496491

497492
Primaries

Doc/reference/simple_stmts.rst

Lines changed: 15 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -445,53 +445,26 @@ The :keyword:`yield` statement
445445
.. productionlist::
446446
yield_stmt: `yield_expression`
447447

448-
The :keyword:`yield` statement is only used when defining a generator function,
449-
and is only used in the body of the generator function. Using a :keyword:`yield`
450-
statement in a function definition is sufficient to cause that definition to
451-
create a generator function instead of a normal function.
452-
453-
When a generator function is called, it returns an iterator known as a generator
454-
iterator, or more commonly, a generator. The body of the generator function is
455-
executed by calling the :func:`next` function on the generator repeatedly until
456-
it raises an exception.
457-
458-
When a :keyword:`yield` statement is executed, the state of the generator is
459-
frozen and the value of :token:`expression_list` is returned to :meth:`next`'s
460-
caller. By "frozen" we mean that all local state is retained, including the
461-
current bindings of local variables, the instruction pointer, and the internal
462-
evaluation stack: enough information is saved so that the next time :func:`next`
463-
is invoked, the function can proceed exactly as if the :keyword:`yield`
464-
statement were just another external call.
465-
466-
The :keyword:`yield` statement is allowed in the :keyword:`try` clause of a
467-
:keyword:`try` ... :keyword:`finally` construct. If the generator is not
468-
resumed before it is finalized (by reaching a zero reference count or by being
469-
garbage collected), the generator-iterator's :meth:`close` method will be
470-
called, allowing any pending :keyword:`finally` clauses to execute.
471-
472-
When ``yield from <expr>`` is used, it treats the supplied expression as
473-
a subiterator, producing values from it until the underlying iterator is
474-
exhausted.
475-
476-
.. versionchanged:: 3.3
477-
Added ``yield from <expr>`` to delegate control flow to a subiterator
478-
479-
For full details of :keyword:`yield` semantics, refer to the :ref:`yieldexpr`
480-
section.
448+
A :keyword:`yield` statement is semantically equivalent to a :ref:`yield
449+
expression <yieldexpr>`. The yield statement can be used to omit the parentheses
450+
that would otherwise be required in the equivalent yield expression
451+
statement. For example, the yield statements ::
481452

482-
.. seealso::
453+
yield <expr>
454+
yield from <expr>
483455

484-
:pep:`0255` - Simple Generators
485-
The proposal for adding generators and the :keyword:`yield` statement to Python.
456+
are equivalent to the yield expression statements ::
486457

487-
:pep:`0342` - Coroutines via Enhanced Generators
488-
The proposal to enhance the API and syntax of generators, making them
489-
usable as simple coroutines.
458+
(yield <expr>)
459+
(yield from <expr>)
490460

491-
:pep:`0380` - Syntax for Delegating to a Subgenerator
492-
The proposal to introduce the :token:`yield_from` syntax, making delegation
493-
to sub-generators easy.
461+
Yield expressions and statements are only used when defining a :term:`generator`
462+
function, and are only used in the body of the generator function. Using yield
463+
in a function definition is sufficient to cause that definition to create a
464+
generator function instead of a normal function.
494465

466+
For full details of :keyword:`yield` semantics, refer to the
467+
:ref:`yieldexpr` section.
495468

496469
.. _raise:
497470

0 commit comments

Comments
 (0)