Skip to the content.

Function (Method) Decorator

Syntactic Sugar

@decorator
def func(arg1, arg2, ...):
    pass

semantically equivalent:

def func(arg1, arg2, ...):
    pass
f = decorator(func)(arg1, arg2, ...)

Multiple Decorators

@dec2
@dec1
def func(arg1, arg2, ...):
    pass

equivalent to:

def func(arg1, arg2, ...):
    pass
func = dec2(dec1(func))(arg1, arg2, ...)

@functools.wraps Implementation Detail

from functools import partial, update_wrapper

WRAPPER_ASSIGNMENTS = ('__module__', '__name__', '__qualname__', '__doc__', '__annotations__')
WRAPPER_UPDATES = ('__dict__',)

def wraps(wrapped,
          assigned = WRAPPER_ASSIGNMENTS,
          updated = WRAPPER_UPDATES):
    return partial(update_wrapper, wrapped=wrapped, assigned=assigned, updated=updated)

References