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 ...

Get Functional Python Programming - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.