Generators, Iterators, and Asynchronous Programming
Generators are another one of those features that makes Python a peculiar language over more traditional ones. In this chapter, we will explore their rationale, why they were introduced in the language, and the problems they solve. We will also cover how to address problems idiomatically by using generators, and how to make our generators (or any iterable, for that matter) Pythonic.
We will understand why iteration (in the form of the iterator pattern) is automatically supported in the language. From there, we will take another journey and explore how generators became such a fundamental feature of Python in order to support other functionality, such as coroutines and asynchronous programming.
The goals of this chapter are as follows:
- To create generators that improve the performance of our programs
- To study how iterators (and the iterator pattern, in particular) are deeply embedded in Python
- To solve...