@@ -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
328326When a generator function is called, it returns an iterator known as a
329327generator. That generator then controls the execution of a generator function.
330328The 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
349347where should the execution continue after it yields; the control is always
350348transferred 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
358356When ``yield from <expr> `` is used, it treats the supplied expression as
359357a 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
382392Generator-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
478487Python."
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
497492Primaries
0 commit comments