Using decorators to modify function behavior before use
Decorators are one of the most common inspection-oriented metaprogramming techniques in Python. Because functions in Python are first-class objects, they can be inspected and modified at runtime. Decorators are special functions capable of inspecting, modifying, or wrapping other functions.
The decorator syntax was explained in Chapter 4, Python in Comparison with Other Languages, and is in fact a syntactic sugar that is supposed to make it easier to work with functions that extend existing code objects with additional behavior.
You can write code that uses the simple decorator syntax as follows:
You can also write it in the following (more verbose) way:
decorated_function = some_decorator(decorated_function)
This verbose form of function decoration clearly shows what the decorator does. It takes a function object...