The first step simple function
1 " " simple function: Call two times " " 2 def myfunc (): 3 Print ('myfunc () called. ' )45myfunc ()6 myfunc ()
The second step adorner provides additional functionality for the calling function
1 " "Replace function (decoration)2 The parameter of the adornment function is the decorated function object, returning the original function object3 The material statement of the ornament; myfunc = Deco (myfunc)" "4 defDeco (func):5 Print("before MyFunc () called.")6 func ()7 Print("After MyFunc () called.")8 returnfunc9 Ten defmyfunc (): One Print("MyFunc () called.") A -MyFunc =deco (MyFunc) - MyFunc () theMyFunc ()
The third step uses the syntax @ to decorate the function
1 " "use the syntax @ to decorate the function, equivalent to MyFunc = Deco (myfun)2 but the new function is called only the first time, and the original function calls once more" "3 defDeco (func):4 Print("before MyFunc () called.")5 func ()6 Print("After MyFunc () called.")7 returnfunc8 9 @decoTen defmyfunc (): One Print("MyFunc () called.") A - MyFunc () -MyFunc ()
Fourth Step
Python Decorator initial learning