import time def timmer(func): #函数名可以当做函数的参数 def inner(): start = time.time() func() end = time.time() print(end - start) return inner def hahaha(): time.sleep(0.1) print(‘aaaa‘) #hahaha() # timmer(hahaha) #我们不能修改这个函数的调用方式 #也不能修改原代码 # hahaha = timmer(hahaha) #timmer函数的地址给了hahaha # hahaha() #实际上执行的是timmer import time def timmer(func): #函数名可以当做函数的参数 def inner(): start = time.time() func() end = time.time() print(end - start) return inner def hahaha(): time.sleep(0.1) print(‘aaaa‘) # hahaha = timmer(hahaha) #timmer函数的地址给了hahaha # hahaha() # def timmer(func): #---> hahaha # def inner(x,y): # func(x,y) # --->hahaha # return inner # # def hahaha(a,b): # time.sleep(0.1) # print(a,b) # # hahaha = timmer(hahaha) # #我们不能修改这个函数的调用方式 # hahaha(1,2) #==>inner(1,2) def timmer(func): #---> hahaha def inner(x,y): func(x,y) # --->hahaha return inner def hahaha(a,b): time.sleep(0.1) print(a,b) def kkk(a): print(a) # hahaha = timmer(hahaha) # hahaha(1,2) #inner(1,2) def timmer(func): #---> hahaha def inner(*args,**kwargs): #args == (1,2) kwargs == {} #*args == 1,2 **kwargs == a =1,b = 2 func(*args,**kwargs) # --->hahaha return inner def kkk(a): print(a) kkk = timmer(kkk) # kkk(1) def timmer(func): #---> jjj def inner(*args,**kwargs): ret = func(*args,**kwargs) # --->ret = jjj() return ret return inner @timmer #jjj = timmer(jjj) 语法糖 def jjj(): return 123 # jjj = timmer(jjj) ret = jjj() #==>inner #装饰器的本质 :闭包函数 #功能:就是在不改变原函数调用方式的情况下,在这个函数前后加上扩展功能 def timmer(func): def inner(*args,**kwargs): ‘‘‘添加函数调用之前的扩展代码‘‘‘ ret = func(*args,**kwargs) ‘‘‘添加函数调用之后的扩展代码‘‘‘ return ret return inner #设计模式 原则 开放封闭原则 #对扩展是开放的 #对修改是封闭的 def wrapper(func):#装饰 def inner(*args,**kwargs): ret = func(*args,**kwargs) return ret return inner @wrapper def aaa(): print(‘asghksdlhf‘) @wrapper def bbb(): print(‘asghksdlhf‘) # aaa() # bbb() # aaa() # bbb() import time def wrapper(func): # 装饰 def inner(*args, **kwargs): start = time.time() ret = func(*args, **kwargs) end = time.time() print(end - start) return ret return inner @wrapper def lll(): time.sleep(0.1) print(‘hello‘) lll()
一、带参数的装饰器;------开关
__author__ = ‘Administrator‘ F=True def outer(F): def wap(fun):#gg def inner(*args,**kwargs): if F: print("inner before") ret=fun(*args,**kwargs)#gg() print("inner after") else: ret=fun(*args,**kwargs) return ret return inner return wap @outer(F) #先执行out(F) ----@wap----->gg=wap(gg) def gg(): print("gg............") gg()#inner()
二、多个装饰器装饰一个函数
__author__ = ‘Administrator‘ def yn(fun):#fun=sc_inner def yn_inner(*args,**kwargs): print("yun..........before") ret=fun(*args,**kwargs)#sc_inner() print("yn...........after") return ret return yn_inner def sx(fun):#fun=china def sx_inner(*args,**kwargs): print("sx..........before") ret=fun(*args,**kwargs)#china() print("sx...........after") return ret return sx_inner @yn #china=yn(china)-----china=yn(sx(china))----china=yn(sx_inner)---->china=yn_ineer @sx #china=sx(china)----china= def china(): print("china is good ") china()#yn_inner()
三、装饰器的一个应用
l = [] def wrapper(func): l.append(func) #统计当前程序中有多少个函数被装饰了 def inner(*args,**kwargs): #l.append(func) #统计本次程序执行有多少个带这个装饰器的函数被调用了 ret = func(*args,**kwargs) return ret return inner @wrapper #f1 = wrapper(f1) def f1(): print(‘in f1‘) @wrapper #f2 = wrapper(f2) def f2(): print(‘in f2‘) @wrapper #f2 = wrapper(f2) def f3(): print(‘in f3‘) print(l)