We can also use a class as a decorator in Python. The function will be received as an argument to __init__
and the function arguments will be passed to __call__
.
In the example below we define StoreResults
which stores the results of every call to add()
.
Note that when we use a class decorator, the function we are decorating becomes an object of the class and can access all its methods and attributes of it. Hence, we can access the results
list by using add.results
as seen above.