Decorators allow the programmer to redirect a function call in a predictable and manageable fashion. The following is an example of the basic syntax.
The goal of this new syntax is to make it easier for programmers to see the interrelationship between methods.
class Class: Â Â Â Â def method(cls) Â Â Â Â Â Â Â Â ... Â Â Â Â @method Â Â Â Â def function: Â Â Â Â Â Â Â Â pass
An incidental plus is that one can knit two different pieces of code together very cleanly without editing either the function or the function call. Simply create a function to match the call and have it decorated to the desired function.
This is the basic syntax, but a concrete example is really needed to illustrate this feature. The following pages look at the use of single and multiple decorators for functions and the value they hold for Python programming.