Skip to content

Commit

Permalink
bpo-46033: Clarify for-statement execution (GH-30025)
Browse files Browse the repository at this point in the history
Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
  • Loading branch information
3 people committed Apr 3, 2022
1 parent 01be5d6 commit 281f980
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
19 changes: 8 additions & 11 deletions Doc/reference/compound_stmts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -157,17 +157,14 @@ The :keyword:`for` statement is used to iterate over the elements of a sequence
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 ``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.
The ``starred_list`` expression is evaluated once; it should yield an
:term:`iterable` object. An :term:`iterator` is created for that iterable.
The first item provided
by the iterator is then assigned to the target list using the standard
rules for assignments (see :ref:`assignment`), and the suite is executed. This
repeats for each item provided by the iterator. When the iterator is exhausted,
the suite in the :keyword:`!else` clause,
if present, is executed, and the loop terminates.

.. index::
statement: break
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Clarify ``for`` statement execution in its doc.

0 comments on commit 281f980

Please sign in to comment.