Next / Previous / Contents / TCC Help System / NM Tech homepage

24.4. Decorators

The purpose of a Python decorator is to replace a function or method with a modified version at the time it is defined. For example, the original way to declare a static method was like this:

    def someMethod(x, y):
        ...
    someMethod = staticmethod(someMethod)

Using Python's decorator syntax, you can get the same effect like this:

    @staticmethod
    def someMethod(x, y):
         ...

In general, a function or method may be preceded by any number of decorator expressions, and you may also provide arguments to the decorators.

Here is an example of a function wrapped with two decorators, of which the second has additional arguments:

@f1
@f2('Pewty')
def f0(...):
    ...

This is the equivalent code without using decorators:

def f0(...):
    ...
f0 = f1 ( f2('Pewty') ( f0 ) )

First function f2 is called with one argument, the string 'Pewty'. The return value, which must be callable, is then called with f0 as its argument. The return value from that call is then passed to f1. Name f0 is then bound to the return value from the call to f1.