1.3.10 · D1Python Intermediate

Foundations — Iterators — `__iter__`, `__next__`, StopIteration

1,596 words7 min readBack to topic

Before you can read the parent note Iterators — `__iter__`, `__next__`, StopIteration, you must own every word and symbol it silently assumes. This page builds each one from zero, in an order where each idea rests on the one before it.


0. What even is an "object" and a "method"?

Picture a vending machine. The data is "how many cans are inside". The methods are "press button", "return coins". You never reach inside — you press buttons. Methods are those buttons.

Figure — Iterators — `__iter__`, `__next__`, StopIteration

1. Dunder names: __like_this__

Read __next__ out loud as "dunder next". The underscores are Python's way of saying: "this is a hook the language itself will call for you."

You write Python secretly calls
len(x) x.__len__()
x + y x.__add__(y)
iter(x) x.__iter__()
next(x) x.__next__()

2. self — the object talking about itself

Picture the vending machine holding a little note that says "I have 4 cans". When a method reads self.current, it is reading its own note, not some other machine's.


3. Returning vs raising

Picture return as placing a candy in your friend's hand. Picture raise as ringing an alarm bell — nobody gets candy; instead everyone reacts to the bell.

Figure — Iterators — `__iter__`, `__next__`, StopIteration

4. StopIteration — the "tape ended" signal

Picture a cassette tape. Every press of next plays a note. When the tape runs out, pressing again makes a small click — that click is StopIteration. The listener hears the click and stops pressing.


5. try / except — listening for the signal

Picture a smoke detector wired to one action: if the alarm rings (except StopIteration), then quietly leave the room (break). No alarm → keep working.


6. while True and break — the pressing loop

Picture a person pressing the next button over and over (while True) until they hear the click, at which point they stop pressing and step away (break).


7. Putting the vocabulary together

Now every symbol in the parent's core loop is defined. Read it again — nothing is magic:

Every word here is one of Sections 1–6. See for loops and comprehensions for how this same machinery powers comprehensions.


8. Iterable vs iterator — two roles, pictured

Figure — Iterators — `__iter__`, `__next__`, StopIteration

Prerequisite map

objects and methods

dunder methods

self stores state

iter calls __iter__

next calls __next__

return vs raise

StopIteration signal

try except break

iterable vs iterator

Iterator protocol

Read it top-to-bottom: objects give us dunders and self; those give us iter/next; return vs raise gives us the StopIteration signal caught by try/except; together they define the protocol. For composing iterators safely, continue to itertools — islice, count, chain.


Equipment checklist

Cover each answer and test yourself. If any one is fuzzy, reread its section before the parent note.

What is a method, in one sentence?
A function that lives inside an object and is called with a dot, like a button on a machine.
What does a name like __next__ (a "dunder") signal?
It is a hook Python calls automatically in response to syntax such as next(x).
Inside a method, what does self refer to?
The very object the method belongs to — where its remembered state lives.
Difference between return v and raise E?
return hands a value back and succeeds; raise throws a signal up the call stack until something catches it.
What is StopIteration for?
It is the agreed exception meaning "no more items" — the end-of-tape click, not an error.
What does try: / except StopIteration: let a loop do?
Attempt next(), and calmly stop (break) exactly when the empty signal is raised.
What does break do inside while True:?
Immediately exits the otherwise-forever loop.
Iterable vs iterator, in the candy metaphor?
Iterable = bag that hands out fresh dispensers; iterator = the dispenser itself that remembers its position.
Why must a list NOT be its own iterator?
So each iter(list) gives an independent position, letting nested loops over the same list work.