A decorator can change the attributes of a function. Here is the list of attributes of a function:
__doc__ |
The docstring, or none |
__name__ |
The original name of the function |
__module__ |
The name of the module the function was defined in, or none |
__qualname__ |
The function's fully-qualified name, __module__.__name__ |
__defaults__ |
The default argument values, or none if there are no defaults |
__kwdefaults__ |
The default values for keyword-only parameters |
__code__ |
The code object representing the compiled function body |
__dict__ |
A namespace for the function's attributes |
__annotations__ |
The annotations of parameters, including 'return' for the return annotation |
__globals__ |
The global namespace of the module that the function was defined in; this is used to resolve global variables and is read-only |
__closure__ |
Bindings for the function's free variables or none; it is read-only |
Except for __globals__ and __closure__, a decorator can change any of these attributes. As a practical matter, it's best to only copy the __name__ and __doc__ from the original function to the decorated function. Most of the other attributes, while changeable, are easier to manage with a simple technique of defining a new function inside the decorator and returning the new function. We'll look into this in the following several examples.
Now, let's see how to construct a decorated class.