Python, from Zero, for AILesson 64 of 89

Module 7 · Objects, generators and the shape a larger program takes

The iterator protocol: what for actually does, and why a generator is empty the second time

In this lesson

  • 9 min read
  • 7 reading sections
  • Knowledge check
  • Lesson 5 of 10 in this chapter

This chapter’s goalRestructure a growing script into classes and dataclasses with a swappable provider behind one interface, implement the dunder methods that let your objects work with for, len, in and with, write a decorator that retries or caches, run a type checker and act on its output, make the project installable as a command with pyproject.toml, and profile it to say from measurement rather than guesswork where the time goes.

This chapter’s media

The film explains chapter ideas. The audiobook narrates the complete teaching text.

FormatEnglishहिंदी
Chapter filmWatch1:40Not available
Full audiobookNot availableNot available
What a for loop actually doesfor x in thingsTwo functioncalls inside ahidden whileiter(things),onceCalls __iter__and gets aniteratornext(it), eachpassRuns to the nextyield and bindsthe value to xStopIterationCaught by foritself, which iswhy you neversee itA second loop?A list hands outa freshiterator; agenerator isused upBecause a generator is its own iterator, the second loop over it starts where the first one stopped,which is at the end. There is no error and no warning; the loop body simply never runs. If you needthe data twice, materialise it once with list(), or write a function you can call again.
Because a generator is its own iterator, the second loop over it starts where the first one stopped, which is at the end. There is no error and no warning; the loop body simply never runs. If you need the data twice, materialise it once with list(), or write a function you can call again.

Figure

A little at a time

Explore an idea, try it, then check what stays.

Step 1 of 9Free · no account needed
Lesson steps

Two calls under every loop

Python
for item in things:
    ...

is shorthand for:

Python
it = iter(things)
while True:
    try:
        item = next(it)
    except StopIteration:
        break
    ...

iter(things) calls things.__iter__() and gets back an iterator: an object with a __next__ method. next(it) calls it. When there is nothing left, __next__ raises StopIteration and the loop ends. That is the entire protocol, and every for, every comprehension, every sum, list, max, zip and enumerate runs on it.

Two roles are in play. An iterable is anything iter() accepts: a list, a string, a dict, a file, your Conversation with its __iter__. An iterator is the thing that hands out items one at a time and remembers where it is. The distinction seems academic until it bites.

Your place and first choices stay on this browser. Classroom credit is recorded separately.

Stuck on something?Ask Divya, your AI tutor

How to cite: Addaly (2026) ‘The iterator protocol: what for actually does, and why a generator is empty the second time’, in Python, from Zero, for AI. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/python-for-ai/the-iterator-protocol (Accessed: 21 November 2026).