@@ -133,7 +133,7 @@ Parenthesized forms
133133A parenthesized form is an optional expression list enclosed in parentheses:
134134
135135.. productionlist ::
136- parenth_form: "(" [`expression_list `] ")"
136+ parenth_form: "(" [`starred_expression `] ")"
137137
138138A parenthesized expression list yields whatever that expression list yields: if
139139the list contains at least one comma, it yields a tuple; otherwise, it yields
@@ -202,7 +202,7 @@ A list display is a possibly empty series of expressions enclosed in square
202202brackets:
203203
204204.. productionlist ::
205- list_display: "[" [`expression_list ` | `comprehension `] "]"
205+ list_display: "[" [`starred_list ` | `comprehension `] "]"
206206
207207A list display yields a new list object, the contents being specified by either
208208a list of expressions or a comprehension. When a comma-separated list of
@@ -223,7 +223,7 @@ A set display is denoted by curly braces and distinguishable from dictionary
223223displays by the lack of colons separating keys and values:
224224
225225.. productionlist ::
226- set_display: "{" (`expression_list ` | `comprehension `) "}"
226+ set_display: "{" (`starred_list ` | `comprehension `) "}"
227227
228228A set display yields a new mutable set object, the contents being specified by
229229either a sequence of expressions or a comprehension. When a comma-separated
@@ -250,7 +250,7 @@ curly braces:
250250.. productionlist ::
251251 dict_display: "{" [`key_datum_list ` | `dict_comprehension `] "}"
252252 key_datum_list: `key_datum ` ("," `key_datum `)* [","]
253- key_datum: `expression ` ":" `expression `
253+ key_datum: `expression ` ":" `expression ` | "**" ` or_expr `
254254 dict_comprehension: `expression ` ":" `expression ` `comp_for `
255255
256256A dictionary display yields a new dictionary object.
@@ -261,6 +261,16 @@ used as a key into the dictionary to store the corresponding datum. This means
261261that you can specify the same key multiple times in the key/datum list, and the
262262final dictionary's value for that key will be the last one given.
263263
264+ .. index :: unpacking; dictionary, **; in dictionary displays
265+
266+ A double asterisk ``** `` denotes :dfn: `dictionary unpacking `.
267+ Its operand must be a :term: `mapping `. Each mapping item is added
268+ to the new dictionary. Later values replace values already set by
269+ earlier key/datum pairs and earlier dictionary unpackings.
270+
271+ .. versionadded :: 3.5
272+ Unpacking into dictionary displays, originally proposed by :pep: `448 `.
273+
264274A dict comprehension, in contrast to list and set comprehensions, needs two
265275expressions separated with a colon followed by the usual "for" and "if" clauses.
266276When the comprehension is run, the resulting key and value elements are inserted
@@ -649,15 +659,15 @@ series of :term:`arguments <argument>`:
649659
650660.. productionlist ::
651661 call: `primary ` "(" [`argument_list ` [","] | `comprehension `] ")"
652- argument_list: `positional_arguments ` ["," `keyword_arguments `]
653- : ["," "*" ` expression `] ["," ` keyword_arguments `]
654- : ["," "**" ` expression `]
655- : | `keyword_arguments ` ["," "*" ` expression `]
656- : ["," ` keyword_arguments `] [ "," "**" `expression `]
657- : | "*" `expression ` ["," ` keyword_arguments `] ["," "**" ` expression `]
658- : | "** " `expression `
659- positional_arguments: ` expression ` (", " `expression `)*
660- keyword_arguments: `keyword_item ` (", " `keyword_item `)*
662+ argument_list: `positional_arguments ` ["," `starred_and_keywords `]
663+ : ["," ` keywords_arguments `]
664+ : | ` starred_and_keywords ` ["," ` keywords_arguments `]
665+ : | `keywords_arguments `
666+ positional_arguments: ["*"] ` expression ` ( "," ["*"] `expression `)*
667+ starred_and_keywords: ( "*" `expression ` | ` keyword_item `)
668+ : ("," "*" `expression ` | "," ` keyword_item `)*
669+ keywords_arguments: (` keyword_item ` | "** " `expression `)
670+ : ("," `keyword_item ` | "** " `expression `)*
661671 keyword_item: `identifier ` "=" `expression `
662672
663673An optional trailing comma may be present after the positional and keyword arguments
@@ -715,17 +725,18 @@ there were no excess keyword arguments.
715725
716726.. index ::
717727 single: *; in function calls
728+ single: unpacking; in function calls
718729
719730If the syntax ``*expression `` appears in the function call, ``expression `` must
720- evaluate to an iterable. Elements from this iterable are treated as if they
721- were additional positional arguments; if there are positional arguments
722- * x1 *, ... , *xN *, and `` expression `` evaluates to a sequence *y1 *, ..., *yM *,
723- this is equivalent to a call with M+N positional arguments *x1 *, ..., * xN *,
724- *y1 *, ..., *yM *.
731+ evaluate to an :term: ` iterable ` . Elements from these iterables are
732+ treated as if they were additional positional arguments. For the call
733+ `` f(x1, x2 , *y, x3, x4) ``, if * y * evaluates to a sequence *y1 *, ..., *yM *,
734+ this is equivalent to a call with M+4 positional arguments *x1 *, * x2 *,
735+ *y1 *, ..., *yM *, * x3 *, * x4 * .
725736
726737A consequence of this is that although the ``*expression `` syntax may appear
727- *after * some keyword arguments, it is processed *before * the keyword arguments
728- (and the ``**expression `` argument, if any -- see below). So::
738+ *after * explicit keyword arguments, it is processed *before * the
739+ keyword arguments (and any ``**expression `` arguments -- see below). So::
729740
730741 >>> def f(a, b):
731742 ... print(a, b)
@@ -746,13 +757,20 @@ used in the same call, so in practice this confusion does not arise.
746757 single: **; in function calls
747758
748759If the syntax ``**expression `` appears in the function call, ``expression `` must
749- evaluate to a mapping, the contents of which are treated as additional keyword
750- arguments. In the case of a keyword appearing in both ``expression `` and as an
751- explicit keyword argument, a :exc: `TypeError ` exception is raised.
760+ evaluate to a :term: `mapping `, the contents of which are treated as
761+ additional keyword arguments. If a keyword is already present
762+ (as an explicit keyword argument, or from another unpacking),
763+ a :exc: `TypeError ` exception is raised.
752764
753765Formal parameters using the syntax ``*identifier `` or ``**identifier `` cannot be
754766used as positional argument slots or as keyword argument names.
755767
768+ .. versionchanged :: 3.5
769+ Function calls accept any number of ``* `` and ``** `` unpackings,
770+ positional arguments may follow iterable unpackings (``* ``),
771+ and keyword arguments may follow dictionary unpackings (``** ``).
772+ Originally proposed by :pep: `448 `.
773+
756774A call always returns some value, possibly ``None ``, unless it raises an
757775exception. How this value is computed depends on the type of the callable
758776object.
@@ -1407,13 +1425,29 @@ Expression lists
14071425
14081426.. productionlist ::
14091427 expression_list: `expression ` ( "," `expression ` )* [","]
1428+ starred_list: `starred_item ` ( "," `starred_item ` )* [","]
1429+ starred_expression: `expression ` | ( `starred_item ` "," )* [`starred_item `]
1430+ starred_item: `expression ` | "*" `or_expr `
14101431
14111432.. index :: object: tuple
14121433
1413- An expression list containing at least one comma yields a tuple. The length of
1434+ Except when part of a list or set display, an expression list
1435+ containing at least one comma yields a tuple. The length of
14141436the tuple is the number of expressions in the list. The expressions are
14151437evaluated from left to right.
14161438
1439+ .. index ::
1440+ pair: iterable; unpacking
1441+ single: *; in expression lists
1442+
1443+ An asterisk ``* `` denotes :dfn: `iterable unpacking `. Its operand must be
1444+ an :term: `iterable `. The iterable is expanded into a sequence of items,
1445+ which are included in the new tuple, list, or set, at the site of
1446+ the unpacking.
1447+
1448+ .. versionadded :: 3.5
1449+ Iterable unpacking in expression lists, originally proposed by :pep: `448 `.
1450+
14171451.. index :: pair: trailing; comma
14181452
14191453The trailing comma is required only to create a single tuple (a.k.a. a
0 commit comments