__next__
__next__ is a Python special method that returns an iterator’s next item or raises StopIteration when no items remain.
Python Workout: 50 ten-minute exercises
__next__ is a Python special method that returns an iterator's next value and signals exhaustion when no values remain.
- How it is used
- Uses __next__ to implement custom iteration and explain how for loops obtain values.
- How deep it goes
- Explains state, advancement, exhaustion, and reusable iterator designs, while deferring broader iterator variations to exercises.
- Examples
- Demonstrates LoudIterator, MyEnumerate, Circle, generator expressions, and generator functions.
- What you can do after
- Builds custom iterators that return values, maintain position, and signal exhaustion with StopIteration.
Read how Python Workout: 50 ten-minute exercises explains __next__ →
Python Workout, Second Edition
The __next__ method is Python's special method that returns the next value from an iterator or raises StopIteration when no values remain.
- How it is used
- Implements the value-by-value production step underlying Python for loops and direct next() calls.
- How deep it goes
- Explains state storage, boundary checks, returned values, StopIteration, and separate iterator objects, but defers generator internals.
- Examples
- Demonstrates LoudIterator, MyEnumerate, Circle, generator expressions, and a custom range-like iterator.
- What you can do after
- Builds a class-based iterator that tracks progress, returns values, and terminates correctly with 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.
- StopIterationproduces · StopIteration is the Python exception raised when an iterator or generator has no further values to produce.
- nextused by · An iterator operation that advances an iterator and returns its next value or result, with behavior on exhaustion determined by the API.
- 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.

