Using Generators

Generators are another 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 for 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 problems that involve iteration idiomatically
  • To understand how generators work as the basis for coroutines and asynchronous programming
  • To explore the syntactic support for coroutines—yield from, await, and async def
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.64.89