Mastering decorators: the cherry on top of your functions
- Track:
- Python Core, Internals, Extensions
- Type:
- Tutorial
- Level:
- intermediate
- Duration:
- 180 minutes
Abstract
The decorator pattern is a functional pattern that Python developers leverage to write more modular and composable functions.
This means that your functions end up being shorter and more focused, which is good since a function should do one thing and it should do it well.
Then, you can use decorators to introduce extra useful functionality that is not part of the spec of the function. For example, you can add caching, profiling, or logging. Moreover, because you do this with decorators, you can reuse that logic for other functions.
In this hands-on tutorial you will learn how to determine when decorators should be used and how to write your own decorators. This will help you write code that is easier to reason about and maintain.
By the time you are done, you will have implemented a dozen decorators, you will know the full anatomy of a decorator, and you will have explored common useful decorators from the standard library and more.