@@ -284,7 +284,7 @@ A list display is a possibly empty series of expressions enclosed in square
284
284
brackets:
285
285
286
286
.. productionlist :: python-grammar
287
- list_display: "[" [`starred_list ` | `comprehension `] "]"
287
+ list_display: "[" [`flexible_expression_list ` | `comprehension `] "]"
288
288
289
289
A list display yields a new list object, the contents being specified by either
290
290
a list of expressions or a comprehension. When a comma-separated list of
@@ -309,7 +309,7 @@ A set display is denoted by curly braces and distinguishable from dictionary
309
309
displays by the lack of colons separating keys and values:
310
310
311
311
.. productionlist :: python-grammar
312
- set_display: "{" (`starred_list ` | `comprehension `) "}"
312
+ set_display: "{" (`flexible_expression_list ` | `comprehension `) "}"
313
313
314
314
A set display yields a new mutable set object, the contents being specified by
315
315
either a sequence of expressions or a comprehension. When a comma-separated
@@ -454,7 +454,7 @@ Yield expressions
454
454
.. productionlist :: python-grammar
455
455
yield_atom: "(" `yield_expression ` ")"
456
456
yield_from: "yield" "from" `expression `
457
- yield_expression: "yield" `expression_list ` | `yield_from `
457
+ yield_expression: "yield" `yield_list ` | `yield_from `
458
458
459
459
The yield expression is used when defining a :term: `generator ` function
460
460
or an :term: `asynchronous generator ` function and
@@ -485,9 +485,9 @@ When a generator function is called, it returns an iterator known as a
485
485
generator. That generator then controls the execution of the generator
486
486
function. The execution starts when one of the generator's methods is called.
487
487
At that time, the execution proceeds to the first yield expression, where it is
488
- suspended again, returning the value of :token: `~python-grammar:expression_list `
488
+ suspended again, returning the value of :token: `~python-grammar:yield_list `
489
489
to the generator's caller,
490
- or ``None `` if :token: `~python-grammar:expression_list ` is omitted.
490
+ or ``None `` if :token: `~python-grammar:yield_list ` is omitted.
491
491
By suspended, we mean that all local state is
492
492
retained, including the current bindings of local variables, the instruction
493
493
pointer, the internal evaluation stack, and the state of any exception handling.
@@ -576,7 +576,7 @@ is already executing raises a :exc:`ValueError` exception.
576
576
:meth: `~generator.__next__ ` method, the current yield expression always
577
577
evaluates to :const: `None `. The execution then continues to the next yield
578
578
expression, where the generator is suspended again, and the value of the
579
- :token: `~python-grammar:expression_list ` is returned to :meth: `__next__ `'s
579
+ :token: `~python-grammar:yield_list ` is returned to :meth: `__next__ `'s
580
580
caller. If the generator exits without yielding another value, a
581
581
:exc: `StopIteration ` exception is raised.
582
582
@@ -695,7 +695,7 @@ how a generator object would be used in a :keyword:`for` statement.
695
695
Calling one of the asynchronous generator's methods returns an :term: `awaitable `
696
696
object, and the execution starts when this object is awaited on. At that time,
697
697
the execution proceeds to the first yield expression, where it is suspended
698
- again, returning the value of :token: `~python-grammar:expression_list ` to the
698
+ again, returning the value of :token: `~python-grammar:yield_list ` to the
699
699
awaiting coroutine. As with a generator, suspension means that all local state
700
700
is retained, including the current bindings of local variables, the instruction
701
701
pointer, the internal evaluation stack, and the state of any exception handling.
@@ -759,7 +759,7 @@ which are used to control the execution of a generator function.
759
759
asynchronous generator function is resumed with an :meth: `~agen.__anext__ `
760
760
method, the current yield expression always evaluates to :const: `None ` in the
761
761
returned awaitable, which when run will continue to the next yield
762
- expression. The value of the :token: `~python-grammar:expression_list ` of the
762
+ expression. The value of the :token: `~python-grammar:yield_list ` of the
763
763
yield expression is the value of the :exc: `StopIteration ` exception raised by
764
764
the completing coroutine. If the asynchronous generator exits without
765
765
yielding another value, the awaitable instead raises a
@@ -892,7 +892,7 @@ will generally select an element from the container. The subscription of a
892
892
:ref: `GenericAlias <types-genericalias >` object.
893
893
894
894
.. productionlist :: python-grammar
895
- subscription: `primary ` "[" `expression_list ` "]"
895
+ subscription: `primary ` "[" `flexible_expression_list ` "]"
896
896
897
897
When an object is subscripted, the interpreter will evaluate the primary and
898
898
the expression list.
@@ -904,9 +904,13 @@ primary is subscripted, the evaluated result of the expression list will be
904
904
passed to one of these methods. For more details on when ``__class_getitem__ ``
905
905
is called instead of ``__getitem__ ``, see :ref: `classgetitem-versus-getitem `.
906
906
907
- If the expression list contains at least one comma, it will evaluate to a
908
- :class: `tuple ` containing the items of the expression list. Otherwise, the
909
- expression list will evaluate to the value of the list's sole member.
907
+ If the expression list contains at least one comma, or if any of the expressions
908
+ are starred, the expression list will evaluate to a :class: `tuple ` containing
909
+ the items of the expression list. Otherwise, the expression list will evaluate
910
+ to the value of the list's sole member.
911
+
912
+ .. versionchanged :: 3.11
913
+ Expressions in an expression list may be starred. See :pep: `646 `.
910
914
911
915
For built-in objects, there are two types of objects that support subscription
912
916
via :meth: `~object.__getitem__ `:
@@ -1905,10 +1909,12 @@ Expression lists
1905
1909
single: , (comma); expression list
1906
1910
1907
1911
.. productionlist :: python-grammar
1912
+ starred_expression: ["*"] `or_expr `
1913
+ flexible_expression: `assignment_expression ` | `starred_expression `
1914
+ flexible_expression_list: `flexible_expression ` ("," `flexible_expression `)* [","]
1915
+ starred_expression_list: `starred_expression ` ("," `starred_expression `)* [","]
1908
1916
expression_list: `expression ` ("," `expression `)* [","]
1909
- starred_list: `starred_item ` ("," `starred_item `)* [","]
1910
- starred_expression: `expression ` | (`starred_item ` ",")* [`starred_item `]
1911
- starred_item: `assignment_expression ` | "*" `or_expr `
1917
+ yield_list: `expression_list ` | `starred_expression ` "," [`starred_expression_list `]
1912
1918
1913
1919
.. index :: pair: object; tuple
1914
1920
@@ -1929,6 +1935,9 @@ the unpacking.
1929
1935
.. versionadded :: 3.5
1930
1936
Iterable unpacking in expression lists, originally proposed by :pep: `448 `.
1931
1937
1938
+ .. versionadded :: 3.11
1939
+ Any item in an expression list may be starred. See :pep: `646 `.
1940
+
1932
1941
.. index :: pair: trailing; comma
1933
1942
1934
1943
A trailing comma is required only to create a one-item tuple,
0 commit comments