Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7fb94fd

Browse files
authored
bpo-46725: Document starred expressions in for statements (GH-31481)
Automerge-Triggered-By: GH:pablogsal
1 parent09487c1 commit7fb94fd

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

‎Doc/reference/compound_stmts.rst‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,17 +154,20 @@ The :keyword:`for` statement is used to iterate over the elements of a sequence
154154
(such as a string, tuple or list) or other iterable object:
155155

156156
..productionlist::python-grammar
157-
for_stmt: "for" `target_list` "in" `expression_list` ":" `suite`
157+
for_stmt: "for" `target_list` "in" `starred_list` ":" `suite`
158158
: ["else" ":" `suite`]
159159

160160
The expression list is evaluated once; it should yield an iterable object. An
161-
iterator is created for the result of the ``expression_list``. The suite is
162-
then executed once for each item provided by the iterator, in the order returned
163-
by the iterator. Each item in turn is assigned to the target list using the
164-
standard rules for assignments (see:ref:`assignment`), and then the suite is
165-
executed. When the items are exhausted (which is immediately when the sequence
166-
is empty or an iterator raises a:exc:`StopIteration` exception), the suite in
167-
the:keyword:`!else` clause, if present, is executed, and the loop terminates.
161+
iterator is created for the result of the ``starred_list``. The expression
162+
list can contain starred elements (``*x, *y``) that will be unpacked in the
163+
final iterator (as when constructing a ``tuple`` or ``list`` literal). The
164+
suite is then executed once for each item provided by the iterator, in the
165+
order returned by the iterator. Each item in turn is assigned to the target
166+
list using the standard rules for assignments (see:ref:`assignment`), and then
167+
the suite is executed. When the items are exhausted (which is immediately when
168+
the sequence is empty or an iterator raises a:exc:`StopIteration` exception),
169+
the suite in the:keyword:`!else` clause, if present, is executed, and the loop
170+
terminates.
168171

169172
..index::
170173
statement: break
@@ -196,6 +199,8 @@ the built-in function :func:`range` returns an iterator of integers suitable to
196199
emulate the effect of Pascal's ``for i := a to b do``; e.g., ``list(range(3))``
197200
returns the list ``[0, 1, 2]``.
198201

202+
..versionchanged::3.11
203+
Starred elements are now allowed in the expression list.
199204

200205
.. _try:
201206
.. _except:

‎Doc/whatsnew/3.11.rst‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ traceback. (Contributed by Irit Katriel in :issue:`45607`.)
160160
Other Language Changes
161161
======================
162162

163+
* Starred expressions can be used in:ref:`for statements<for>`. (See
164+
:issue:`46725` for more details.)
165+
163166
* Asynchronous comprehensions are now allowed inside comprehensions in
164167
asynchronous functions. Outer comprehensions implicitly become
165168
asynchronous. (Contributed by Serhiy Storchaka in:issue:`33346`.)

‎Lib/test/test_grammar.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,6 +1403,12 @@ def __getitem__(self, i):
14031403
result.append(x)
14041404
self.assertEqual(result, [1,2,3])
14051405

1406+
result= []
1407+
a=b=c= [1,2,3]
1408+
forxin*a,*b,*c:
1409+
result.append(x)
1410+
self.assertEqual(result,3*a)
1411+
14061412
deftest_try(self):
14071413
### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
14081414
### | 'try' ':' suite 'finally' ':' suite

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp