7 ms·
It is confusing, but it's also kind of the standard pattern for decorators in Python, so it's a useful example. The decorator syntax itself is just simple synt
by ptx 3y ago
It is confusing, but it's also kind of the standard pattern for decorators in Python, so it's a useful example.
The decorator syntax itself is just simple syntax sugar for replacing the decorated function (add) with the return value of the decorator function (show_call). The example is essentially equivalent to this:
def show_call(func):
...
def add(x, y):
return x + y
add = show_call(add)
So show_call has to return the function we want as a replacement, which is the nested "out" function here. The "@wraps" thing is not strictly necessary but updates the new function object to have the same name as the original.
- Spivak 3y agoYeah the standard "I'm gonna make a decorator for this" template is def decorate(<decorator args>): def _decorate(<thing to be decorated>): pass return _decorate @decorate(foo=1) def myfunc(): pass