Skip to content
Forge Learn/Functional Python & Iterators

Iterators & the Iterator Protocol

6 min read

You'll learn to

  • -Explain what makes an object "iterable" versus an "iterator"
  • -Use `iter()` and `next()` directly, and understand `StopIteration`
  • -Implement `__iter__` and `__next__` to make a custom class iterable

You have written dozens of `for item in some_list:` loops without needing to know what happens underneath. This chapter opens up that machinery - it is a small amount of new vocabulary that immediately explains generators (next chapter) and lets you make your own classes work seamlessly with `for` loops.

Iterable vs. Iterator

An iterable is anything you can call `iter()` on to get an iterator - lists, tuples, strings, dicts, sets, files. An iterator is the object that actually produces values one at a time, via `next()`, and raises `StopIteration` when there is nothing left. A `for` loop is just convenient syntax that calls `iter()` once, then `next()` repeatedly, catching `StopIteration` for you automatically.

What a for loop does under the hood

A list is iterable, but it is not itself an iterator - `iter([1, 2, 3])` returns a separate list_iterator object. That is why you can have two independent `for` loops over the same list running at different positions at once.

Implementing __iter__ and __next__ on a Custom Class

To make your own class work in a `for` loop, implement `__iter__` (returning an iterator - often `self`) and `__next__` (returning the next value, or raising `StopIteration` when exhausted).

A custom class implementing the iterator protocol
  • -`iter(obj)` calls `obj.__iter__()` - it produces an iterator.
  • -`next(it)` calls `it.__next__()` - it produces the next value or raises `StopIteration`.
  • -A `for` loop is sugar for calling `iter()` once and `next()` repeatedly until `StopIteration`.
  • -Any class implementing both `__iter__` and `__next__` works directly with `for`, `list(...)`, `sum(...)`, and every other function that expects an iterable.
ScaleDojo Logo
Initializing ScaleDojo