4 ms·
Decorators take a function and return a new function. 9 times out of 10 they're used as an easy way to create wrappers: do something and then execute the origin
by stdbrouw 14y ago
Decorators take a function and return a new function. 9 times out of 10 they're used as an easy way to create wrappers: do something and then execute the original function, or execute the original function and then process it before handing it off.
They're doubly useful in Python because Python has only limited support for functions as blocks.
In JavaScript you'd do:
var myFunction = addLogging(function(){ ... })
But in Python you'd do
@log
def my_function():
...