In this chapter, we've seen a number of the advanced features of functions. We've looked at the essential generator expression and how this is used as part of a comprehension. A list
comprehension assembles a list
from the generated values. Similarly, a set
comprehension creates a set
. A dictionary comprehension creates a dict
structure from the keys and values in a generator expression.
We've looked at using the yield
statement to create a generator function. This allows us to use all of the various Python statement features when creating a generator. Since a generator is iterable, it works with a for
loop so that we can write a simple loop to process multiple values created by an iterator.
We've also looked at higher-order functions. These are functions which take functions as arguments or produce functions as a result. With higher-order functions, we can refactor our algorithms into functions that can be combined to create the desired behavior.
In Chapter 9, Exceptions, we'll look...