What is a python adorner?
An adorner is actually a function, a function that wraps a function, returns a modified function object, assigns it the original identifier, and permanently loses access to the original function object.
eg: when you need to add the same functionality to FUNC1 and FUNC2, you can add the entire function once in the outer. The way the adorner connects to a function is done with the @+ adorner name in the previous line of the function . And be sure to return the decorated object in the adorner.
def outer (fun): Def wrapper (): print ' verify ' fun () print ' Zhuangshiq ' return wrapper# be sure to return an adorner object @outer #装饰器与函数建立连接def Func1 (): print ' func1 ' @outerdef Func2 (): print ' Func2 ' Func1 () Func2 ()
Adorner parameters:
If there are accepted arguments in the function, you must add a parameter to the adorner. A function call is also added to the function call inside the adorner.
def outer (fun): Def wrapper (ARG): #arg为形参 print ' verify ' Print arg fun (arg) print ' Zhuangshiq ' Return wrapper @outer #装饰器与函数建立连接def Func1 (ARG): print ' func1 ', Arg@outerdef Func2 (ARG): print ' Func2 ', arg Func1 (' a ') Func2 (' a ')
Python Decorator Learning Notes