mirror of
https://github.com/python/cpython.git
synced 2025-11-15 00:00:00 +00:00
bpo-46033: Clarify for-statement execution (GH-30025)
Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu> Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
This commit is contained in:
parent
01be5d6446
commit
281f980d35
2 changed files with 9 additions and 11 deletions
|
|
@ -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`
|
for_stmt: "for" `target_list` "in" `starred_list` ":" `suite`
|
||||||
: ["else" ":" `suite`]
|
: ["else" ":" `suite`]
|
||||||
|
|
||||||
The expression list is evaluated once; it should yield an iterable object. An
|
The ``starred_list`` expression is evaluated once; it should yield an
|
||||||
iterator is created for the result of the ``starred_list``. The expression
|
:term:`iterable` object. An :term:`iterator` is created for that iterable.
|
||||||
list can contain starred elements (``*x, *y``) that will be unpacked in the
|
The first item provided
|
||||||
final iterator (as when constructing a ``tuple`` or ``list`` literal). The
|
by the iterator is then assigned to the target list using the standard
|
||||||
suite is then executed once for each item provided by the iterator, in the
|
rules for assignments (see :ref:`assignment`), and the suite is executed. This
|
||||||
order returned by the iterator. Each item in turn is assigned to the target
|
repeats for each item provided by the iterator. When the iterator is exhausted,
|
||||||
list using the standard rules for assignments (see :ref:`assignment`), and then
|
the suite in the :keyword:`!else` clause,
|
||||||
the suite is executed. When the items are exhausted (which is immediately when
|
if present, is executed, and the loop terminates.
|
||||||
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::
|
.. index::
|
||||||
statement: break
|
statement: break
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
Clarify ``for`` statement execution in its doc.
|
||||||
Loading…
Add table
Add a link
Reference in a new issue