A decorator is a callable that manipulates and returns a function.
# wraps decorator copies metadata of decorated function (func) to wrapped function (out)
from functools import wraps
def show_call(func):
"""
Print function name and arguments each time it is called.
"""
@wraps(func)
def out(*args, **kwds):
print(func.__name__, args, kwds)
return func(*args, **kwds)
return out
@show_call
def add(x, y):
return x + y
This example uses a decorator function show_call() when calling function add(), but that decorator function itself is not a simple function, but some weirdo that actually defines another function out() with *args and **kwargs THAT IS ALSO ALREADY DECORATED (with function wraps())!I couldn't have thought of a worse example for a cheat sheet.
A cheat sheet is supposed to give short simple explanations/demonstrations.
That example shouldn't have a nested function, it should demonstrate just 1 decorator function and 1 decorated function and none of these functions should be nested and they also shouldn't use arguments with asterisks (if possible).
Feedback from someone still failing to understand what decorator is, especially after reading the cheat sheet.