Summary
Decorators are powerful tools in Python that can be applied to many things such as classes, methods, functions, generators, and many more. We have demonstrated how to create decorators in different ways, and for different purposes, and drew some conclusions along the way.
When creating a decorator for functions, try to make its signature match the original function being decorated. Instead of using the generic *args, and **kwargs, making the signature match the original one will make it easier to read, and maintain, and it will resemble the original function more closely, so it will be more familiar to readers of that code.
Decorators are a very useful tool for reusing code and following the DRY principle. However, their usefulness ...
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