@@ -450,21 +450,20 @@ functions are described separately in section
450
450
:ref: `asynchronous-generator-functions `.
451
451
452
452
When a generator function is called, it returns an iterator known as a
453
- generator. That generator then controls the execution of the generator function.
454
- The execution starts when one of the generator's methods is called. At that
455
- time, the execution proceeds to the first yield expression, where it is
456
- suspended again, returning the value of :token: `expression_list ` to the generator's
457
- caller. By suspended, we mean that all local state is retained, including the
458
- current bindings of local variables, the instruction pointer, the internal
459
- evaluation stack, and the state of any exception handling. When the execution
460
- is resumed by calling one of the
461
- generator's methods, the function can proceed exactly as if the yield expression
462
- were just another external call. The value of the yield expression after
463
- resuming depends on the method which resumed the execution. If
464
- :meth: `~generator.__next__ ` is used (typically via either a :keyword: `for ` or
465
- the :func: `next ` builtin) then the result is :const: `None `. Otherwise, if
466
- :meth: `~generator.send ` is used, then the result will be the value passed in to
467
- that method.
453
+ generator. That generator then controls the execution of the generator
454
+ function. The execution starts when one of the generator's methods is called.
455
+ At that time, the execution proceeds to the first yield expression, where it is
456
+ suspended again, returning the value of :token: `~python-grammar:expression_list `
457
+ to the generator's caller. By suspended, we mean that all local state is
458
+ retained, including the current bindings of local variables, the instruction
459
+ pointer, the internal evaluation stack, and the state of any exception handling.
460
+ When the execution is resumed by calling one of the generator's methods, the
461
+ function can proceed exactly as if the yield expression were just another
462
+ external call. The value of the yield expression after resuming depends on the
463
+ method which resumed the execution. If :meth: `~generator.__next__ ` is used
464
+ (typically via either a :keyword: `for ` or the :func: `next ` builtin) then the
465
+ result is :const: `None `. Otherwise, if :meth: `~generator.send ` is used, then
466
+ the result will be the value passed in to that method.
468
467
469
468
.. index :: single: coroutine
470
469
@@ -514,8 +513,8 @@ on the right hand side of an assignment statement.
514
513
usable as simple coroutines.
515
514
516
515
:pep: `380 ` - Syntax for Delegating to a Subgenerator
517
- The proposal to introduce the :token: `yield_from ` syntax, making delegation
518
- to subgenerators easy.
516
+ The proposal to introduce the :token: `~python-grammar: yield_from ` syntax,
517
+ making delegation to subgenerators easy.
519
518
520
519
:pep: `525 ` - Asynchronous Generators
521
520
The proposal that expanded on :pep: `492 ` by adding generator capabilities to
@@ -543,9 +542,9 @@ is already executing raises a :exc:`ValueError` exception.
543
542
:meth: `~generator.__next__ ` method, the current yield expression always
544
543
evaluates to :const: `None `. The execution then continues to the next yield
545
544
expression, where the generator is suspended again, and the value of the
546
- :token: `expression_list ` is returned to :meth: `__next__ `'s caller. If the
547
- generator exits without yielding another value, a :exc: ` StopIteration `
548
- exception is raised.
545
+ :token: `~python-grammar: expression_list ` is returned to :meth: `__next__ `'s
546
+ caller. If the generator exits without yielding another value, a
547
+ :exc: ` StopIteration ` exception is raised.
549
548
550
549
This method is normally called implicitly, e.g. by a :keyword: `for ` loop, or
551
550
by the built-in :func: `next ` function.
@@ -634,21 +633,20 @@ An asynchronous generator object is typically used in an
634
633
:keyword: `async for ` statement in a coroutine function analogously to
635
634
how a generator object would be used in a :keyword: `for ` statement.
636
635
637
- Calling one of the asynchronous generator's methods returns an
638
- :term: `awaitable ` object, and the execution starts when this object
639
- is awaited on. At that time, the execution proceeds to the first yield
640
- expression, where it is suspended again, returning the value of
641
- :token: `expression_list ` to the awaiting coroutine. As with a generator,
642
- suspension means that all local state is retained, including the
643
- current bindings of local variables, the instruction pointer, the internal
644
- evaluation stack, and the state of any exception handling. When the execution
645
- is resumed by awaiting on the next object returned by the asynchronous
646
- generator's methods, the function can proceed exactly as if the yield
647
- expression were just another external call. The value of the yield expression
648
- after resuming depends on the method which resumed the execution. If
636
+ Calling one of the asynchronous generator's methods returns an :term: `awaitable `
637
+ object, and the execution starts when this object is awaited on. At that time,
638
+ the execution proceeds to the first yield expression, where it is suspended
639
+ again, returning the value of :token: `~python-grammar:expression_list ` to the
640
+ awaiting coroutine. As with a generator, suspension means that all local state
641
+ is retained, including the current bindings of local variables, the instruction
642
+ pointer, the internal evaluation stack, and the state of any exception handling.
643
+ When the execution is resumed by awaiting on the next object returned by the
644
+ asynchronous generator's methods, the function can proceed exactly as if the
645
+ yield expression were just another external call. The value of the yield
646
+ expression after resuming depends on the method which resumed the execution. If
649
647
:meth: `~agen.__anext__ ` is used then the result is :const: `None `. Otherwise, if
650
- :meth: `~agen.asend ` is used, then the result will be the value passed in to
651
- that method.
648
+ :meth: `~agen.asend ` is used, then the result will be the value passed in to that
649
+ method.
652
650
653
651
If an asynchronous generator happens to exit early by :keyword: `break `, the caller
654
652
task being cancelled, or other exceptions, the generator's async cleanup code
@@ -700,10 +698,10 @@ which are used to control the execution of a generator function.
700
698
Returns an awaitable which when run starts to execute the asynchronous
701
699
generator or resumes it at the last executed yield expression. When an
702
700
asynchronous generator function is resumed with an :meth: `~agen.__anext__ `
703
- method, the current yield expression always evaluates to :const: `None ` in
704
- the returned awaitable, which when run will continue to the next yield
705
- expression. The value of the :token: `expression_list ` of the yield
706
- expression is the value of the :exc: `StopIteration ` exception raised by
701
+ method, the current yield expression always evaluates to :const: `None ` in the
702
+ returned awaitable, which when run will continue to the next yield
703
+ expression. The value of the :token: `~python-grammar: expression_list ` of the
704
+ yield expression is the value of the :exc: `StopIteration ` exception raised by
707
705
the completing coroutine. If the asynchronous generator exits without
708
706
yielding another value, the awaitable instead raises a
709
707
:exc: `StopAsyncIteration ` exception, signalling that the asynchronous
@@ -1706,8 +1704,9 @@ Assignment expressions
1706
1704
assignment_expression: [`identifier ` ":="] `expression `
1707
1705
1708
1706
An assignment expression (sometimes also called a "named expression" or
1709
- "walrus") assigns an :token: `expression ` to an :token: `identifier `, while also
1710
- returning the value of the :token: `expression `.
1707
+ "walrus") assigns an :token: `~python-grammar:expression ` to an
1708
+ :token: `~python-grammar:identifier `, while also returning the value of the
1709
+ :token: `~python-grammar:expression `.
1711
1710
1712
1711
One common use case is when handling matched regular expressions:
1713
1712
0 commit comments