]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-46725: Document starred expressions in for statements (GH-31481)
authorPablo Galindo Salgado <Pablogsal@gmail.com>
Tue, 22 Feb 2022 17:26:46 +0000 (17:26 +0000)
committerGitHub <noreply@github.com>
Tue, 22 Feb 2022 17:26:46 +0000 (09:26 -0800)
Automerge-Triggered-By: GH:pablogsal
Doc/reference/compound_stmts.rst
Doc/whatsnew/3.11.rst
Lib/test/test_grammar.py

index c1ee20e5aee62781d087c371f845bf2771c25a5d..15a51e7fc7beadbb6ad791e08b1ae58d6aac3106 100644 (file)
@@ -154,17 +154,20 @@ The :keyword:`for` statement is used to iterate over the elements of a sequence
 (such as a string, tuple or list) or other iterable object:
 
 .. productionlist:: python-grammar
-   for_stmt: "for" `target_list` "in" `expression_list` ":" `suite`
+   for_stmt: "for" `target_list` "in" `starred_list` ":" `suite`
            : ["else" ":" `suite`]
 
 The expression list is evaluated once; it should yield an iterable object.  An
-iterator is created for the result of the ``expression_list``.  The suite is
-then executed once for each item provided by the iterator, in the order returned
-by the iterator.  Each item in turn is assigned to the target list using the
-standard rules for assignments (see :ref:`assignment`), and then the suite is
-executed.  When the items are exhausted (which is immediately when the sequence
-is empty or an iterator raises a :exc:`StopIteration` exception), the suite in
-the :keyword:`!else` clause, if present, is executed, and the loop terminates.
+iterator is created for the result of the ``starred_list``.  The expression
+list can contain starred elements (``*x, *y``) that will be unpacked in the
+final iterator (as when constructing a ``tuple`` or ``list`` literal). The
+suite is then executed once for each item provided by the iterator, in the
+order returned by the iterator.  Each item in turn is assigned to the target
+list using the standard rules for assignments (see :ref:`assignment`), and then
+the suite is executed.  When the items are exhausted (which is immediately when
+the sequence is empty or an iterator raises a :exc:`StopIteration` exception),
+the suite in the :keyword:`!else` clause, if present, is executed, and the loop
+terminates.
 
 .. index::
    statement: break
@@ -196,6 +199,8 @@ the built-in function :func:`range` returns an iterator of integers suitable to
 emulate the effect of Pascal's ``for i := a to b do``; e.g., ``list(range(3))``
 returns the list ``[0, 1, 2]``.
 
+.. versionchanged:: 3.11
+   Starred elements are now allowed in the expression list.
 
 .. _try:
 .. _except:
index 98fa7a7da466dab4b20b4d64c41bf9b71aceec41..85f12fe8b4fc7f1511bd700f60c8c1ba004cda62 100644 (file)
@@ -160,6 +160,9 @@ traceback. (Contributed by Irit Katriel in :issue:`45607`.)
 Other Language Changes
 ======================
 
+* Starred expressions can be used in :ref:`for statements<for>`. (See
+  :issue:`46725` for more details.)
+
 * Asynchronous comprehensions are now allowed inside comprehensions in
   asynchronous functions. Outer comprehensions implicitly become
   asynchronous. (Contributed by Serhiy Storchaka in :issue:`33346`.)
index 3f5a91a851d8ed2e839f333d61fceb5fa913853c..d35530e4803fa8402db6adb16d32ad7967474d92 100644 (file)
@@ -1403,6 +1403,12 @@ class GrammarTests(unittest.TestCase):
             result.append(x)
         self.assertEqual(result, [1, 2, 3])
 
+        result = []
+        a = b = c = [1, 2, 3]
+        for x in *a, *b, *c:
+            result.append(x)
+        self.assertEqual(result, 3 * a)
+
     def test_try(self):
         ### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
         ###         | 'try' ':' suite 'finally' ':' suite