Decorator Design Techniques
- As an explicit operation that returns a new function, possibly with
a new name:
def original_function():
pass
original_function= decorator(original_function)
These are two different syntaxes for the same operation. The prefix notation has the
advantages of being tidy and succinct. The prefix location is more visible to some
readers. The suffix notation is explicit and slightly more flexible. While the prefix
notation is common, there is one reason for using the suffix notation: we might
not want the resulting function to replace the original function. We might want to
execute the following command that allows us to use both the decorated and the
undecorated functions:
new_function = decorator(original_function)
Python functions are first-class objects. A function that accepts a function as an
argument and returns a function as the result is clearly a built-in feature of the
language. The open question then is how do we update or adjust the internal code
structure of a function?
The answer is we don't.
Rather than messing about on the inside of the code, it's much cleaner to define a
new function that wraps the original function. We have two tiers of higher-order
functions involved in defining a decorator as follows:
- The decorator function applies a wrapper to a base function and returns the
new wrapper. This function can do some one-time only evaluation as part of
building the decorated function. - The wrapper function can (and usually does) evaluate the base function.
This function will be evaluated every time the decorated function is evaluated.
Here's an example of a simple decorator:
from functools import wraps
def nullable(function):
@wraps(function)
def null_wrapper(arg):
return None if arg is None else function(arg)
return null_wrapper