AlgoMaster Logo

__iter__ & __next__

Last Updated: May 22, 2026

High Priority
10 min read

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.

Premium Content

Subscribe to unlock full access to this content and more premium articles.