StopIteration
StopIteration is the Python exception raised when an iterator or generator has no further values to produce.
Python Workout: 50 ten-minute exercises
StopIteration is the Python exception raised by an exhausted iterator to signal that no further values are available.
- How it is used
- Signals iterator exhaustion so for loops and other consumers can terminate cleanly.
- How deep it goes
- Defines the protocol, traces custom implementation, contrasts helper iterators, and explains generator completion behavior.
- Examples
- Uses a traced LoudIterator, a helper iterator for repeated loops, generator functions, and a repeating Circle iterator.
- What you can do after
- Build it from scratch by implementing finite, reusable, or repeating Python iterators.
Read how Python Workout: 50 ten-minute exercises explains StopIteration →
Python Workout, Second Edition
StopIteration is the Python exception raised by an exhausted iterator to signal that no more values are available.
- How it is used
- Explains how Python for loops detect exhausted iterators and how custom iterators signal completion.
- How deep it goes
- Explains __iter__, __next__, stored iteration state, custom classes, and generator termination, but does not pursue advanced iterator designs fully.
- Examples
- Uses strings, lists, dictionaries, files, a traced LoudIterator, generator functions, and a chained generator.
- What you can do after
- Build iterable classes that return values from __next__ and terminate correctly with StopIteration.
Read how Python Workout, Second Edition explains StopIteration →
Related concepts
- iteratorpart of · An iterator is an object or abstraction that provides successive values from a sequence or data source through a traversal protocol.
- __next__produced by · __next__ is a Python special method that returns an iterator’s next item or raises StopIteration when no items remain.
- exceptionkind of · An exception is a runtime signal or object representing an abnormal condition that interrupts ordinary program flow and may be handled or propagated.
- loopaddresses · A loop is a programming control-flow construct that repeatedly executes a block of instructions according to an iteration rule or continuation condition.
- generatorused by · A generator is a language construct or computation that produces values incrementally on demand, typically suspending and resuming while preserving its state.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.

