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 →

Also covered in

What to know first

Related concepts

Questions about StopIteration

What is StopIteration?

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

Which Manning books explain StopIteration?

Python Workout: 50 ten-minute exercises and Python Workout, Second Edition.

What should I know before learning StopIteration?

Manning books that teach StopIteration usually introduce tuple, comprehension, loop and line first.

What is StopIteration a kind of?

StopIteration is a kind of exception. An exception is a runtime signal or value representing an abnormal condition that interrupts ordinary program flow and can be handled or propagated.

What is StopIteration part of?

StopIteration is part of iterator. An iterator is an object or abstraction that provides successive values from a collection or other data source through a traversal interface.

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.