Writing higher-order functions in Python
Higher-order functions can be divided into the following three categories.
- Functions that take functions as arguments.
- Functions that return functions, such as the
Callable
class. Functions that return generator expressions can also be considered higher-order functions. - Functions that take functions as arguments and return functions, such as
functools.partial()
.
We will use higher-order functions to process simple data, change its structure, and perform the following transformations:
- Packaging data to create more complex objects;
- Extracting components from complex data objects;
- Flattening complex data sequences;
- Structuring one-dimensional data sequences.
Callable
class objects are commonly used for functions that return callable objects. We’ll use them later to write functions that can inject configuration parameters.