Skip to content
Forge Learn/Functional Python & Iterators
Browsing as a guest. Sign in to save your progress and earn XP as you complete chapters.

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.

Interview Signal is part of Pro

See a real weak answer next to a real strong one for this exact topic.

Quiz is part of Pro

Test what you just read with a short quiz, and bank the XP.