Skip to content

Commit 281f980

Browse files
michcio1234terryjreedyJelleZijlstra
authored
bpo-46033: Clarify for-statement execution (GH-30025)
Co-authored-by: Terry Jan Reedy <[email protected]> Co-authored-by: Jelle Zijlstra <[email protected]>
1 parent 01be5d6 commit 281f980

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

Doc/reference/compound_stmts.rst

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -157,17 +157,14 @@ The :keyword:`for` statement is used to iterate over the elements of a sequence
157157
for_stmt: "for" `target_list` "in" `starred_list` ":" `suite`
158158
: ["else" ":" `suite`]
159159

160-
The expression list is evaluated once; it should yield an iterable object. An
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.
160+
The ``starred_list`` expression is evaluated once; it should yield an
161+
:term:`iterable` object. An :term:`iterator` is created for that iterable.
162+
The first item provided
163+
by the iterator is then assigned to the target list using the standard
164+
rules for assignments (see :ref:`assignment`), and the suite is executed. This
165+
repeats for each item provided by the iterator. When the iterator is exhausted,
166+
the suite in the :keyword:`!else` clause,
167+
if present, is executed, and the loop terminates.
171168

172169
.. index::
173170
statement: break
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Clarify ``for`` statement execution in its doc.

0 commit comments

Comments
 (0)