One: function decoration function
def Wrapfun (func): def Inner (A, b): Print ('function name:', func. __name__ ) = func (A, b) return R return Inner@wrapfundef Myadd (A, B): return A + bprint( Myadd (2, 3))
Second: Function Decoration class
defWrapclass (CLS):defInner (a):Print('class Name:'Cls.__name__) returnCLS (a)returnInner@wrapclassclassFoo ():def __init__(Self, a): SELF.A=adefFun (self):Print('SELF.A =', SELF.A) m= Foo ('Xiemanr') M.fun ()
Three: Class decoration function
classshowfunname ():def __init__(Self, func): Self._func=funcdef __call__(Self, a):Print('function Name:', Self._func.__name__) returnSelf._func (a) @ShowFunNamedefBar (a):returnaPrint(Bar ('Xiemanr'))
Four: Class decoration class
classShowclassname (object):def __init__(self, CLS): Self._cls=CLSdef __call__(Self, a):Print('class Name:', Self._cls.__name__) returnSelf._cls (a) @ShowClassNameclassFoobar (object):def __init__(Self, a): Self.value=adefFun (self):Print(Self.value) a= Foobar ('Xiemanr') A.fun ()
Reference
Python Primer python 4 types: function decoration function, function decoration class, class decoration function, class decoration class