装饰器的简易版本
# 统计函数的执行时间 import time def index(): time.sleep(2) print('from index') def home(): time.sleep(3) print('from home') def outer(func): # func = index def get_time(): # 1. 统计一下函数执行前的时间戳 start_time = time.time() func() # 2. 统计一下函数执行完之后的时间戳 end_time = time.time() # 3. 计算差值 print('函数执行时间:%s' % (end_time - start_time)) return get_time index = outer(index) # res=>get_time的内存地址 index() # get_time()
装饰器进阶版本
def outer(func): # func = index def get_time(*args, **kwargs): # 1. 统计一下函数执行前的时间戳 start_time = time.time() func(*args, **kwargs) # login() # 2. 统计一下函数执行完之后的时间戳 end_time = time.time() # 3. 计算差值 print('函数执行时间:%s' % (end_time - start_time)) return get_time login = outer(login) # res=>get_time的内存地址 login('ly') # get_time() index=outer(index) index()
解决函数返回值问题
def login(name): time.sleep(1) print('from home', name) return 'from login' def outer(func): # func = index def get_time(*args, **kwargs): # 1. 统计一下函数执行前的时间戳 start_time = time.time() res=func(*args, **kwargs) # login() # 2. 统计一下函数执行完之后的时间戳 end_time = time.time() # 3. 计算差值 print('函数执行时间:%s' % (end_time - start_time)) return res return get_time login = outer(login) # res=>get_time的内存地址 res=login('ly') # get_time() print(res)
装饰器练习题
# 调用index函数,调用之前,需要输入用户名和密码,并且,用户名和密码必须输入正确 def home(): print('from home') def login(): print('from login') is_auth = {'is_login': False} def login_auth(func): # func = index def auth(): # 判断是否已经认证成功了 if is_auth.get('username'): res = func() return res # 1. 让用户输入用户名和密码 username = input('username:').strip() password = input('password:').strip() # 2. 判断用户名和密码 if username == 'ly' and password == '123': func() is_auth['is_login'] = True else: print('输入错误,不能调用函数') return auth # 一定别忘记把内层函数的函数名返回出去 # auth(index) # auth(home) index = login_auth(index) # res => auth index() # auth() home = login_auth(home) home() login = login_auth(login) login()
装饰器固定模板
def outer(func): def inner(*args, **kwargs): print('函数执行之前要执行的代码') res = func(*args, **kwargs) print('函数执行之后要执行的代码') return res return inner
装饰器的语法糖
def outer(func): def inner(): start_time = time.time() res = func() end_time = time.time() print('函数执行时间:%s' % (end_time - start_time)) return res return inner # 统计函数的执行时间 @outer # # index = outer(index) def index(): time.sleep(3) print('from index') index() ''' 装饰器语法糖的执行流程: 把语法糖下面紧贴着的函数名当成参数传递给装饰器函数参数 ''' @outer # home = outer(home) def home(): print('from home') home()
装饰器的双层语法糖
# 统计函数执行时间的装饰器 import time def get_time(func): def inner(): start_time = time.time() res = func() end_time = time.time() print('函数执行时间:%s' % (end_time - start_time)) return res return inner # 认证装饰器 def login_auth(func): # func = index def auth(): # 1. 让用户输入用户名和密码 username = input('username:').strip() password = input('password:').strip() # 2. 判断用户名和密码 if username == 'ly' and password == '123': func() # inner() else: print('输入错误,不能调用函数') return auth # 一定别忘记把内层函数的函数名返回出去 @login_auth # index=login_auth(inner) #index=>auth的内存地址 @get_time # inner=get_time(index) def index(): # def auth() time.sleep(2) print('from index') index() # auth()
装饰器双层语法糖的练习题
# 判断七句print执行顺序 def outter1(func1): print('加载了outter1') def wrapper1(*args, **kwargs): print('执行了wrapper1') res1 = func1(*args, **kwargs) return res1 return wrapper1 def outter2(func2): print('加载了outter2') def wrapper2(*args, **kwargs): print('执行了wrapper2') res2 = func2(*args, **kwargs) return res2 return wrapper2 def outter3(func3): print('加载了outter3') def wrapper3(*args, **kwargs): print('执行了wrapper3') res3 = func3(*args, **kwargs) return res3 return wrapper3 @outter1 @outter2 @outter3 def index(): print('from index')
有参装饰器
# 认证装饰器 def outter(source_data, a, b,c): # source_data = 'file' def login_auth(func): # func = index def auth( *args, **kwargs): # 1. 让用户输入用户名和密码 # username = input('username:').strip() # password = input('password:').strip() if source_data == 'file': print('从文件中读数据') elif source_data =='mysql': print('从mysql中读取数据') elif source_data == 'oracle': print('从oracle中读取数据') return auth # 一定别忘记把内层函数的函数名返回出去 return login_auth @outter('file', 1, 2,3) # outter('file') @login_auth def index(): pass index()
浙公网安备 33010602011771号