There are four types of adorners that can be analyzed, depending on whether the function has a return value for the argument:
form one: no parameter, no return value
1 defOuter (func):2 defwrapper ():3 Print("Adorner function 1")4 ret = func ()5 Print("Adorner function 2")
return ret6 returnwrapper7 8 #define a function that has no parameter and no return value9 @outerTen defMain_func (): One Print("Hello") A -Main_func ()#Start Execution - the - The results are as follows: - Adorner function 1 - Hello + Adorner function 2 - +Process has ended, exit code 0
Summary: The adorner function 1 adorner function 2 can be based on the specific requirements of the project needs additional functions to write code, you can before the original function function, you can also after it
form two: no parameter and return value
1 defOuter (func):2 defwrapper ():3 Print("Adorner function 1")4RET =func ()5 Print("Adorner function 2")6 returnret7 returnwrapper8 9 #defines a function that has a return value without a parameterTen @outer One defMain_func (): A return "Hello" - - the Print(Main_func () - - - The results are as follows: + Adorner function 1 - Adorner function 2 + Hello A atProcess has ended, exit code 0
Summary: The adorner function 1 adorner function 2 can be based on the specific requirements of the project needs additional functions to write specific code, can be before the original function function, but not after the original function function
Form three: The parameter has no return value
1 defOuter (func):2 defWrapper (STR1):3 Print("Adorner function 1")4RET =func (str1)5 Print("Adorner function 2")6 returnret7 returnwrapper8 9 #defines a function with no return value for the parameterTen @outer One defMain_func (str1): A Print("Hello"+str1) - - theMain_func ("Python") -The results are as follows: The function of the adorner 1 Hello Python Adorner function 2 The process has ended, exit code 0
Summary: The adorner function 1 adorner function 2 can be based on the specific requirements of the project needs additional functions to write code, you can before the original function function, you can also after it
form Four: The parameter has a return value
1 defOuter (func):2 defWrapper (STR1):3 Print("Adorner function 1")4RET =func (str1)5 Print("Adorner function 2")6 returnret7 returnwrapper8 9 #defines a function that has a parameter return valueTen @outer One defMain_func (str1): A return "Hello"+str1 - - the Print(Main_func ("Python")) - - - The results are as follows: + Adorner function 1 - Adorner function 2 + Hello Python A atProcess has ended, exit code 0
Summary: The adorner function 1 adorner function 2 can be based on the specific requirements of the project needs additional functions to write specific code, can be before the original function function, but not after the original function function
Now use the universal decorator to achieve the above four types of adorners:
1 defSet_func (func):2 defWrapper (*args,**Kwargs):3 Print("Adorner function 1")4 returnFunc (*args,**Kwargs)5 returnwrapper6 7 @set_func8 defShow1 ():9 Print("----SHOW1----")Ten One @set_func A defShow2 (string,n,l): - return 'Hello'+ string + str (n) +':'. Join (L) - the Show1 () - Print(Show2 (' World', 2, [' One',' Both','three'])) - - + The results are as follows: - Adorner function 1 +----SHOW1--- A Adorner function 1 at Hello World 2one:two:three - -Process has ended, exit code 0
Python Programming Series---several forms of adorners in Python and all-purpose decorators