Modifier with parametric function
1. Func1 with parametric function
def func1 (ARG): Print arg
2. Decorative Device
def outer (fun): def Wrapper (ARG): Print ' Test ' Fun (ARG) return Wrapper
Adorner outer time return function wrapper, because to func1 with parameters, if you need to take the wrapper function with parameters, func1 is decorated and redefined as:
Func1 (ARG) =wrapper (ARG) = { print'test' func1 (ARG) }
Using function decorations in decorators
The additional functions to be added are
def before (): Print ' before ' def After (): Print ' After '
Principal function:
def Main (): Print ' Main '
Adorner:
defFilter (Before_func,after_func): function name after #要在主体函数前 and functiondefouter (main_func):defwrapper (): Before_result=Before_func () #如果前函数不为None, the executionif(before_result!=None):returnBefore_result Main_result=Main_func () #执行主体函数if(main_result!=None):returnMain_result After_result=After_func () #如果后函数不为None, the executionif(after_result!=None):returnAfter_resultreturnwrapperreturnOuter
Execute the result, print out:
Before
Main
After
Adorners decorate with parameters of functions and add functions