The process of forming an adorner
The simplest adorner----has a return value----has a parameter----the function of the universal parametric adorner:
Do not want to modify the function call method, but also want to add function before and after the function # Principle: Open Closed principle
Open: Open to expansion
Closed: Closed for modification
Syntax sugar: @ Adorner function name, @timmer equivalent to Func = Timmer (func), where Func () is a decorated function, Timmer () is an adorner function
The simplest decorator
Import Timedef Timmer (f): start = Time.time () f () end = Time.time () print (End-start) def func (): Time.sleep (0.1) print (' nice! ') Timmer (func)
Adorners decorated with return values
Import Time#timmer () is an adorner function, just a function with a decorative function Def timmer (f): #通过此闭包函数返回一个内部函数名, and then externally received, so that the received function name and external function name consistency can be def inner (): start = Time.time () ret = f () #被装饰的函数 end = Time.time () print (End-start) return ret return inner@timmer #语法糖 [email protected] Adorner function name, equivalent to Func = Timmer (func) def func (): # decorated function Time.sleep (0.1) print (' nice! ') Return ' Summertrain ' # func = Timmer (func) result = Func () print (result)
has one parameter
Import Time#timmer () is the adorner function Def timmer (f): #通过此闭包函数返回一个内部函数名, and then externally received, so that the received function name and the external function name consistency can be def inner (a): Start = Time.time () ret = f (a) #被装饰的函数 end = Time.time () print (End-start) return ret return Inner@timmer #语法糖 [email protected] Adorner function name, equivalent to Func = Timmer (func) def func (a): # decorated function time.sleep ( 0.1) print (' nice! ', a) return ' Summertrain ' # func = Timmer (func) result = Func (1) print (result)
Universal parameters
#装饰带多个参数, and includes an adorner with the parameters of location and parameter import Time#timmer () is the adorner function Def timmer (f): #通过此闭包函数返回一个内部函数名, and then externally received, so that the received function name is consistent with the external function name def Inner (*args,**kwargs): start = Time.time () ret = f (*args,**kwargs) #被装饰的函数 end = Time.time () print (End-start) return ret return inner@timmer #语法糖 [email protected] Adorner function name, equivalent to Func = Timmer (func) def func (A, B): # decorated function Time.sleep (0.1) print (' nice! ', A, b) return ' Summertrain ' # func = Timmer (func) result = Func (1,b=22) print (result)
The fixed mode of the adorner
#例子import Timedef Wrapper (f): # F is the decorated function def inner (*args,**kwargs): "The thing to do before being decorated function '" ret = f (*args,**kwargs) " The thing after being decorated function " return ret return inner@wrapperdef func (b): Time.sleep (0.1) print (' nice! ', A, b) return ' Summertrain ' #减缩版def Wapper (func): def Inner (*args,**kwargs): ret = func (*args,**kwargs) return ret return inner@wapper#qqxing = Wapper (qqxing) def qqxing (): print (1111) result = qqxing ()
10-3 function Adorner