Python3 function 3
Adorner Decorator * * *
Concept: An adorner is a function that is primarily used to wrap another function or class;
The purpose of packaging is to change the behavior of the wrapped function (object) without changing the name of the original function.
Adorner function: def adorner function name (parameter):
function block
return function
Example:
def deco (FN):
Print ("The adorner function is called and returns the original function")
RETURN fn
With adorner function syntax:
@ Adorner function name [(Adorner function pass parameter)] < line break >
def function name (parameter list):
Statement block
Note: [] representative can omit
1 defDeco (FN):2 Print("the adorner is called and the original function is returned")3 Print(FN)4 returnfn5 #return Lambda:p rint ("Hello,world")6 7 @deco8 defMYFAC ():9 Print("MYFAC is called")Ten One #MYFAC = Deco (MYFAC) A MYFAC () - MYFAC () - # the defDeco (FN): - Print("the adorner is called and the original function is returned") - Print(FN) - #return FN + return Lambda:Print("Hello,world") - + @deco A defMYFAC (): at Print("MYFAC is called") - - #MYFAC = Deco (MYFAC) - MYFAC () - MYFAC () - # in #the adorner is called and the original function is returned - #<function MYFAC at 0x00000000006766a8> to #MYFAC is called + #MYFAC is called - #the adorner is called and the original function is returned the #<function MYFAC at 0x00000000005acb70> * #Hello,world $ #Hello,worldView Code
The path to Python, 12th: Introduction to Python and Basics 12