Writing a simple function decorator
A decorator is a function (or a callable object) that accepts a function as an argument and returns a new function. The result of decoration is a function that has been wrapped. Generally, the additional features of the wrapping surround the original functionality, either by transforming actual argument values or by transforming the result value. These are the two readily available join points in a function.
When we use a decorator, we want to be sure that the resulting decorated function has the original function's name and docstring. These details can be handled for us by a decorator to build our decorators. Using functools.wraps to write new decorators simplifies the work we need to do because the bookkeeping ...
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