From 4600b91d047428207c07b8183749f792f6ce8a43 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Date: Mon, 21 Feb 2022 22:56:11 +0000 Subject: [PATCH 1/4] bpo-46725: Document starred expressions in for statements --- Doc/reference/compound_stmts.rst | 19 +++++++++++-------- Doc/whatsnew/3.11.rst | 2 ++ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/Doc/reference/compound_stmts.rst b/Doc/reference/compound_stmts.rst index c1ee20e5aee627..e6dec643f90fbf 100644 --- a/Doc/reference/compound_stmts.rst +++ b/Doc/reference/compound_stmts.rst @@ -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 diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst index 98fa7a7da466da..01548fd781f4d6 100644 --- a/Doc/whatsnew/3.11.rst +++ b/Doc/whatsnew/3.11.rst @@ -160,6 +160,8 @@ traceback. (Contributed by Irit Katriel in :issue:`45607`.) Other Language Changes ====================== +* Starred expressions can be used in :ref:`for statements`. + * Asynchronous comprehensions are now allowed inside comprehensions in asynchronous functions. Outer comprehensions implicitly become asynchronous. (Contributed by Serhiy Storchaka in :issue:`33346`.) From 87f5e001c6d9428d45cb4712543121a06201690d Mon Sep 17 00:00:00 2001 From: Pablo Galindo Date: Tue, 22 Feb 2022 00:05:37 +0000 Subject: [PATCH 2/4] fixup! bpo-46725: Document starred expressions in for statements --- Doc/reference/compound_stmts.rst | 2 ++ Doc/whatsnew/3.11.rst | 3 ++- Lib/test/test_grammar.py | 6 ++++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Doc/reference/compound_stmts.rst b/Doc/reference/compound_stmts.rst index e6dec643f90fbf..15a51e7fc7bead 100644 --- a/Doc/reference/compound_stmts.rst +++ b/Doc/reference/compound_stmts.rst @@ -199,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: diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst index 01548fd781f4d6..65dec063be4b96 100644 --- a/Doc/whatsnew/3.11.rst +++ b/Doc/whatsnew/3.11.rst @@ -160,7 +160,8 @@ traceback. (Contributed by Irit Katriel in :issue:`45607`.) Other Language Changes ====================== -* Starred expressions can be used in :ref:`for statements`. +* Starred expressions can be used in :ref:`for statements` (See + :issue:`46725` for more details). * Asynchronous comprehensions are now allowed inside comprehensions in asynchronous functions. Outer comprehensions implicitly become diff --git a/Lib/test/test_grammar.py b/Lib/test/test_grammar.py index a2460add4c9eed..d644e847501071 100644 --- a/Lib/test/test_grammar.py +++ b/Lib/test/test_grammar.py @@ -1394,6 +1394,12 @@ def __getitem__(self, i): 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 From b052610b2a30e1a27f09be35f7acdf364fae9cc6 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Salgado Date: Tue, 22 Feb 2022 11:36:19 +0000 Subject: [PATCH 3/4] Update Lib/test/test_grammar.py Co-authored-by: Guido van Rossum --- Lib/test/test_grammar.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_grammar.py b/Lib/test/test_grammar.py index d644e847501071..828cea53237af1 100644 --- a/Lib/test/test_grammar.py +++ b/Lib/test/test_grammar.py @@ -1395,7 +1395,7 @@ def __getitem__(self, i): self.assertEqual(result, [1, 2, 3]) result = [] - a = b = c = [1,2,3] + a = b = c = [1, 2, 3] for x in *a, *b, *c: result.append(x) self.assertEqual(result, 3 * a) From 8efad04cdad5b59aa9e7c44d4ab348ef2e8f5e4c Mon Sep 17 00:00:00 2001 From: Pablo Galindo Salgado Date: Tue, 22 Feb 2022 16:53:56 +0000 Subject: [PATCH 4/4] Update Doc/whatsnew/3.11.rst Co-authored-by: Guido van Rossum --- Doc/whatsnew/3.11.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst index 65dec063be4b96..85f12fe8b4fc7f 100644 --- a/Doc/whatsnew/3.11.rst +++ b/Doc/whatsnew/3.11.rst @@ -160,8 +160,8 @@ traceback. (Contributed by Irit Katriel in :issue:`45607`.) Other Language Changes ====================== -* Starred expressions can be used in :ref:`for statements` (See - :issue:`46725` for more details). +* Starred expressions can be used in :ref:`for statements`. (See + :issue:`46725` for more details.) * Asynchronous comprehensions are now allowed inside comprehensions in asynchronous functions. Outer comprehensions implicitly become