23 Decorators
In this chapter, we’ll explore how behavior implemented in functions and classes can be customized and augmented by wrapper functions. This is especially interesting when the wrapper function performs a generic customization that can be applied in the same way to many different functions or classes. Encapsulation in such a wrapper function then avoids multiple implementations of the same behavior in different functions.
In the following sections, we’ll first develop our own wrapper function using a simple example and then use decorators to introduce an elegant syntax for the function’s use. We’ll then develop a generic cache for function calls in a more complex example and transfer the concept of wrappers and decorators to classes. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access