Python annotator learning notes
What is a python decorator? The decorator is actually a function, a function used to wrap the function, returns a modified function object, and assigns it the original identifier again, and permanently loses access to the original function object. Eg: to add the same functions in Func1 and Func2, you can add all functions in outer once. You can establish a connection between the decorator and the function by using the @ + decorator name in the first line of the function. In addition, you must return the decorated object in the decorator.
Def outer (fun): def wrapper (): print 'Authentication' fun () print 'zhuangshiq' return wrapper # the object of the decorator @ outer # The decorator establishes a connection with the function def Func1 (): print 'func1 '@ outerdef Func2 (): print 'func2' Func1 () func2 ()
Decorator parameters: If the function contains accepted parameters, you must add a parameter to the decorator. In addition, you must add a function call to the function call inside the decorator.
Def outer (fun): def wrapper (arg): # arg is the form parameter print 'to verify 'print arg fun (arg) print 'zhuangshiq' return wrapper @ outer # The decorator establishes a connection with the function def Func1 (arg): print 'func1', arg @ outerdef Func2 (arg): print 'func2 ', arg Func1 ('A') Func2 ('A ')