Skip to content

Commit a1bcd01

Browse files
gh-49989: Add equivalent code for the "for" statement
1 parent 85021bc commit a1bcd01

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Doc/reference/compound_stmts.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,31 @@ statement executed in the first suite skips the rest of the suite and continues
176176
with the next item, or with the :keyword:`!else` clause if there is no next
177177
item.
178178

179+
The following code::
180+
181+
for TARGET in ITER:
182+
SUITE
183+
else:
184+
SUITE2
185+
186+
Is semantically equivalent to::
187+
188+
it = (ITER).__iter__()
189+
running = True
190+
191+
while running:
192+
try:
193+
TARGET = iter.__next__()
194+
except StopIteration:
195+
running = False
196+
else:
197+
SUITE
198+
else:
199+
SUITE2
200+
201+
except that implicit :ref:`special method lookup <special-lookup>` is used
202+
for :meth:`~object.__iter__` and :meth:`~iterator.__next__`.
203+
179204
The for-loop makes assignments to the variables in the target list.
180205
This overwrites all previous assignments to those variables including
181206
those made in the suite of the for-loop::

0 commit comments

Comments
 (0)