Last Updated: May 22, 2026
Python's for loop, comprehensions, * unpacking, and built-ins like sum and min all work on anything that follows a simple contract: the iterator protocol. The contract is two dunder methods, __iter__ and __next__, and one exception, StopIteration. This chapter shows exactly what each piece does, how for desugars into calls to those methods, and the two-argument form of iter() that turns a callable into an iterator.