__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.
Also covered in
What to know first
- tupleA tuple is a finite, ordered collection of values grouped as a single composite value, often with elements of different types.
- loopA loop is a programming-language control-flow construct that repeatedly executes a block of instructions according to an iteration rule or continuation condition.
- constructor methodA constructor method is a type-associated operation involved in creating an instance and establishing its initial state.
- integerAn integer is a whole-number mathematical value, including negative numbers, zero, and positive numbers, with no fractional part.
Related concepts
- iteratorpart of · An iterator is an object or abstraction that provides successive values from a collection or other data source through a traversal interface.
- StopIterationproduces · StopIteration is the Python exception raised when an iterator or generator has no further values to produce.
- generatorused by · A generator is a language construct or computation that produces values incrementally on demand, typically suspending and resuming while preserving its state.
- nextused by · An iterator operation that advances an iterator and returns its next value or result, with behavior on exhaustion determined by the API.
Questions about __next__
What is __next__?
__next__ is a Python special method that returns an iterator’s next item or raises StopIteration when no items remain.
Which Manning books explain __next__?
Python Workout: 50 ten-minute exercises and Python Workout, Second Edition.
What should I know before learning __next__?
Manning books that teach __next__ usually introduce tuple, loop, constructor method and integer first.
What is __next__ part of?
__next__ 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.

