day 11
一、装饰器:
1、什么是装饰器:
装饰器指的是为被装饰对象添加新功能的工具,装饰器本身可以是任意可调用对象。 被装饰对象本身也可以是任意可调用对象。
2、为何要用装饰器:
开放封闭原则:对修改封闭,对扩展开发。
装饰器的实现原则:
a、不能修改被装饰对象的源代码;
b、不能修改被装饰对象的调用方式。
3、装饰器的目的: 就是在遵循原则1和2的前提为被装饰对象添加新功能。
4、简单的装饰器模板
def outter(func): #给inner灵活传入被装饰函数 def inner(*args,**kwargs): #为装饰函数(核心) 并给被装饰函数传入参数 res = func(*args, **kwargs) #调用原函数,病用res接受原函数的返回值 return res #保证用户调用被装饰函数时,返回值仍是原函数的返回值 return inner #将inner的函数地址返回给outter
装饰器实例:
#终版 # import time # import functools # def outter(func): # # func=最原始那个index的内存地址 # @functools.wraps(func)
# def wrapper(*args,**kwargs): # start=time.time() # res=func(*args,**kwargs) #最原始那个index的内存地址() # stop=time.time() # print('run time is %s' %(stop -start)) # return res # return wrapper # # @outter #index=outter(index) #inex=outter(最原始那个index的内存地址) # index=wrapper的内存地址 # def index(): # time.sleep(1) # print('welcome to index page') # return 1234 # # @outter #home=outter(home) # def home(name): # time.sleep(1) # print('welcome %s to home page' %name) # # # res=index() #wrapper的内存地址() # # print('返回值',res) # home('egon')
为函数增加认证功能的装饰器:
import time def auth(func): # func=index def wrapper(*args,**kwargs): name=input('name>>>: ').strip() pwd=input('pwd>>>: ').strip() if name == 'egon' and pwd == '123': print('login successfull') res=func(*args,**kwargs) return res else: print('user or pwd error') return wrapper def outter(func): def wrapper(*args,**kwargs): res=func(*args,**kwargs) return res return wrapper @outter def index(): time.sleep(1) print('welcome to index page') return 1234 index()
装饰器逐级推进:
#原函数: # def index(): # time.sleep(0.5) # print("index is running!") # # index() #第一版:增加计时功能 # def index(): # start_time = time.time() # time.sleep(0.5) # print("index is running!") # stop_time = time.time() # print("运行时间为%s"%(stop_time-start_time)) # index() #第二版:增加计时功能 # def index(): # time.sleep(0.5) # print("index is running!") # # start_time = time.time() # index() # stop_time = time.time() # print("运行时间为%s"%(stop_time-start_time)) #第三版:增加计时功能 # def index(): # time.sleep(0.5) # print("index is running!") # def wapper(): # start_time = time.time() # index() # stop_time = time.time() # print("运行时间为%s"%(stop_time-start_time)) # # wapper() #第四版:增加计时功能 # def index(): # time.sleep(0.5) # print("index is running!") # # def wapper(): # start_time = time.time() # func() # stop_time = time.time() # print("运行时间为%s"%(stop_time-start_time)) # # wapper() #第五版:增加计时功能 # def index(): # time.sleep(0.5) # print("index is running!") # # def outter(func): # def wapper(): # start_time = time.time() # func() # stop_time = time.time() # print("运行时间为%s"%(stop_time-start_time)) # return wapper # # outter(index)() #第六版:增加计时功能 # def index(): # time.sleep(0.5) # print("index is running!") # # def outter(func): # def wapper(): # start_time = time.time() # func() # stop_time = time.time() # print("运行时间为%s"%(stop_time-start_time)) # return wapper # # index = outter(index) # index() #第七版:增加计时功能 # import time # def outter(func): # def wapper(): # start_time = time.time() # func() # stop_time = time.time() # print("运行时间为%s"%(stop_time-start_time)) # return wapper # # @outter # def index(): # time.sleep(0.5) # print("index is running!") # # index()