in skeleton termshere' one common coding pattern that captures this idea--the decorator returns wrapper that retains the original function in an enclosing scopedef decorator( )def wrapper(*args)use and args (*argscalls original function return wrapper on decoration on wrapped function call @decorator def func(xy)func decorator(funcfunc is passed to decorator' func( are passed to wrapper' *args when the name func is later calledit really invokes the wrapper function returned by decoratorthe wrapper function can then run the original func because it is still available in an enclosing scope when coded this wayeach decorated function produces new scope to retain state to do the same with classeswe can overload the call operation and use instance attributes instead of enclosing scopesclass decoratordef __init__(selffunc)on decoration self func func def __call__(self*args)on wrapped function call use self func and args self func(*argscalls original function @decorator def func(xy)func decorator(funcfunc is passed to __init__ func( are passed to __call__' *args when the name func is later called nowit really invokes the __call__ operator overloading method of the instance created by decoratorthe __call__ method can then run the original func because it is still available in an instance attribute when coded this wayeach decorated function produces new instance to retain state supporting method decoration one subtle point about the prior class-based coding is that while it works to intercept simple function callsit does not quite work when applied to class-level method functionsclass decoratordef __init__(selffunc)self func func def __call__(self*args)func is method without instance self is decorator instance the basics