Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Doc/reference/compound_stmts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,31 @@ statement executed in the first suite skips the rest of the suite and continues
with the next item, or with the :keyword:`!else` clause if there is no next
item.

The following code::

for TARGET in ITER:
SUITE
else:
SUITE2

Is semantically equivalent to::

it = (ITER).__iter__()
running = True

while running:
try:
TARGET = iter.__next__()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
TARGET = iter.__next__()
TARGET = it.__next__()

You have it = (ITER).__iter__() above?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I experimented with using iter() and next(), but kept explicit special methods for clarity.

On other hand, iter() is not just calling __iter__(), it works also with sequences without __iter__(), so it may be better to use it for accuracy. Then we have issue with references...

except StopIteration:
running = False
else:
SUITE
else:
SUITE2

except that implicit :ref:`special method lookup <special-lookup>` is used
for :meth:`~object.__iter__` and :meth:`~iterator.__next__`.

The for-loop makes assignments to the variables in the target list.
This overwrites all previous assignments to those variables including
those made in the suite of the for-loop::
Expand Down
Loading