Reference URL:
Various adorners in Python detailed _python_ script house
Http://www.jb51.net/article/63892.htm
One, function-type decorator:
1, the adorner has no parameters, is decorated object without parameters
1 defTest (func):2 def_test ():3 Print('Call the function {0}.'. Format (func.__name__))4 returnfunc ()5 return_test6 7 @test8 defsay ():9 Print('Hello,world')Ten One Say () A - Output: - Call the function say. theHello,world
2, the adorner has no parameters, the decorative object has parameters
1 defTest (func):2 def_test (*args,**kw):3 Print('Call the function {0}.'. Format (func.__name__))4 returnFunc (*args,**kw)5 return_test6 7 @test8 defSay (str1,length):9 Print(Str1[:length])Ten OneSay'Hello,world', 5) A - Output: - Call the function say. theHello
3, the adorner has parameters, is decorated object without parameters
1 defarg_test (str_arg):2 defTest (func):3 def_test ():4 Print('Call the function {0}.'. Format (func.__name__))5 Print(Str_arg)6 returnfunc ()7 return_test8 returnTest9 Ten@arg_test ('With Arg') One defsay (): A Print('Hello,world') - - Say () the - Output: - Call the function say. - With Arg +Hello,world
if the adorner has default parameters, use @ arg_test (), non-parametric adorner directly with @arg_test
4, the adorner has parameters, is decorated object has parameters
1 defarg_test (str_arg):2 defTest (func):3 def_test (*args,**kw):4 Print('Call the function {0}.'. Format (func.__name__))5 Print(Str_arg)6 returnFunc (*args,**kw)7 return_test8 returnTest9 Ten@arg_test ('With Arg') One defSay (str1,length): A Print(Str1[:length]) - -Say'Hello,world', 5) the - Output: - Call the function say. - With Arg +Hello
Second, class decorator
1, the adorner has no parameters, is decorated object without parameters
1 classTest (object):2 def __init__(self,func):3Self._func =func4 5 def __call__(self):6 Print('Call the function {0}.'. Format (Self._func.__name__))7 returnSelf._func ()8 9 @testTen defsay (): One Print('Hello,world') A - @test - defhehe (): the Print('hehe') - - Say () - hehe () + - Output: + Call the function say. A Hello,world at Call the function hehe. -Hehe
2, the adorner has no parameters, the decorative object has parameters
1 classTest (object):2 def __init__(self,func):3Self._func =func4 5 def __call__(self,*args,**kw):6 Print('Call the function {0}.'. Format (Self._func.__name__))7 returnSelf._func (*args,**kw)8 9 @testTen defSay (str1,length): One Print(Str1[:length]) A -Say'Hello,world', 5) - the Output: - Call the function say. -Hello
3, the adorner has parameters, is decorated object without parameters
1 classTest (object):2 def __init__(self,info='De_arg'):3Self._info =Info4 5 def __call__(self,func):6 def __call():7 Print(Self._info)8 Print('Call the function {0}.'. Format (func.__name__))9 returnfunc ()Ten return __call One A - @test () - defsay (): the Print('Hello,world') - - Say () - + Output: -De_arg
Call the function say.
22Hello,world
3, the adorner has parameters, is decorated object has parameters
1 classTest (object):2 def __init__(self,info='De_arg'):3Self._info =Info4 5 def __call__(self,func):6 def __call(*args,**kw):7 Print(Self._info)8 Print('Call the function {0}.'. Format (func.__name__))9 returnFunc (*args,**kw)Ten return __call One A - @test () - defSay (str1,length): the Print(Str1[:length]) - -Say'Hello,world', 5) - + Output: - De_arg + Call the function say. AHello
Python various adorner examples (PYTHON3)