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