12
Decorator Design Techniques
Python offers us many ways to create higher-order functions. In Chapter 5, Higher-Order Functions, we looked at two techniques: defining a function that accepts a function as an argument, and defining a subclass of Callable
, which is either initialized with a function or called with a function as an argument.
One of the benefits of decorating functions is that it can create composite functions. These are single functions that embody functionality from several sources. It’s often helpful to have the decoration syntax as a way to express complex processing.
We can also use decorators to identify classes or functions, often building a registry—a collection of related definitions. We may not necessarily create a composite function when building a registry.
In this chapter, we’ll look at the following topics:
Using a decorator to build a function based on another function
The
wraps()
function in thefunctools
module; this can...