liveBook · Manning

StopIteration

StopIteration is the Python exception raised when an iterator or generator has no further values to produce.

How 2 Manning books explain it

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

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.