NOSSLETTER.techLIVE
back to course

Extending Python's Iterator Protocol with Exception-Based Termination

from: gh-64862: Add the stop_exception parameter in iter() and aiter()

Iterator protocol extension with exception-based termination
The Concept

The iterator protocol in Python traditionally uses sentinel values to signal the end of iteration, but many APIs signal exhaustion by raising exceptions instead. Extending the iterator protocol to accept exceptions as termination signals allows iteration over such callables without awkward workarounds, improving expressiveness and compatibility with more data sources. This approach generalizes iteration to handle both sentinel values and exception-based termination uniformly.

How This PR Does It

This PR enhances both iter() and aiter() by adding a new keyword-only parameter stop_exception, which accepts an exception class or tuple of exception classes that, when raised by the callable, end the iteration gracefully. It also introduces a callable form for aiter(), allowing asynchronous iteration over awaitable callables, which was missing before. The PR carefully normalizes default cases (like StopIteration) to maintain backward compatibility and adds support for pickling iterators with stop_exception via __setstate__. These changes together extend the iterator protocol to handle exception-based termination idiomatically and asynchronously.

Why It Matters

Understanding and using exception-based termination in iteration enables you to work naturally with APIs that signal exhaustion via exceptions, avoiding clumsy sentinel values or manual try-except blocks. This leads to cleaner, more readable code and better integration with asynchronous data sources, which is increasingly important in modern Python applications.

Try It Yourself

Examine a real-world callable or asynchronous callable in your codebase or a third-party library that signals completion by raising an exception rather than returning a sentinel. Refactor your iteration over it using the new stop_exception parameter as shown in the PR examples. How does this change affect the readability and robustness of your iteration logic?