装饰器的介绍
装饰器:本质就是函数 ,功能为其他函数添加附加功能
原则:
1 不修改被修饰函数的源代码
2 不修改被装饰函数的调用方式
装饰器的知识储备
装饰器=高阶函数+函数嵌套+闭包
高阶函数的定义:
1 函数接收的参数是一个函数名
2 函数的返回值是一个函数名
3 满足上述条件之一 就可以称为一个高阶函数
函数嵌套:
在函数里面定义函数
高阶函数
1 import time 2 def foo(): 3 time.sleep(1) 4 print('hello,world') 5 6 def timer(func): 7 start_time = time.time() 8 func() 9 stop_time = time.time() 10 print('函数运行的时间为:%s'%(stop_time-start_time)) 11 return func 12 13 foo = timer(foo) 14 foo()
装饰器的框架:
1 import time 2 def timer (func): 3 def wrapper(): 4 print(func) 5 start_time = time.time() 6 res = func() 7 stop_time = time.time() 8 print('test函数运行的时间是:%s'%(stop_time-start_time)) 9 return res 10 return wrapper 11 @timer 12 def test(): 13 time.sleep(3) 14 print('test函数运行完毕') 15 return 'test的返回值' 16 17 # test = timer(test) #wrapper的地址 @timer 相当于 test = timer(test) 18 ret = test() #运行wrapper函数 19 print(ret)
函数闭包加上认证功能:
1 user_list = [ 2 {'name': 'ball', 'password': '123'}, 3 {'name': 'baby', 'password': '123'}, 4 {'name': 'alex', 'password': '123'}, 5 ] 6 current_dic = {'username': None, 'login': False} # 当前的登录状态 7 8 def auth_func(func): 9 def wrapper(*args, **kwargs): 10 if current_dic['username'] and current_dic['login']: #有用户名登录 执行函数 11 res = func(*args, **kwargs) 12 return res 13 username = input('请输入用户名:').strip() #没有用户名 需登录 14 password = input('请输入密码:').strip() 15 for user_dic in user_list: #判断登录的用户名是列表内的用户 16 if username == user_dic['name'] and password == user_dic['password']: 17 current_dic['username'] = username #修改当前登录状态 18 current_dic['login'] = True 19 res = func(*args, **kwargs) 20 return res 21 else: 22 print('用户名或者密码错误,请重新输入!') 23 return wrapper 24 25 26 @auth_func 27 def index(): 28 print('欢迎来到淘宝首页') 29 30 31 @auth_func 32 def shopping_car(name): 33 print('%s的购物车里有%s%s' % (name, '苹果', '芒果')) 34 35 36 @auth_func 37 def order(): 38 pass 39 40 41 print('befor--->', current_dic) 42 index() 43 print('after-->', current_dic) 44 shopping_car('nihao')
带参函数认证:
1 user_list = [ 2 {'name': 'ball', 'password': '123'}, 3 {'name': 'baby', 'password': '123'}, 4 {'name': 'alex', 'password': '123'}, 5 ] 6 current_dic = {'username': None, 'login': False} # 当前的登录状态 7 def auth(auth_type = 'filedb' ): 8 def auth_func(func): 9 def wrapper(*args, **kwargs): 10 print('认证类型为:',auth_type) 11 if auth_type == 'filedb': 12 if current_dic['username'] and current_dic['login']: #有用户名登录 执行函数 13 res = func(*args, **kwargs) 14 return res 15 username = input('请输入用户名:').strip() #没有用户名 需登录 16 password = input('请输入密码:').strip() 17 for user_dic in user_list: #判断登录的用户名是列表内的用户 18 if username == user_dic['name'] and password == user_dic['password']: 19 current_dic['username'] = username #修改当前登录状态 20 current_dic['login'] = True 21 res = func(*args, **kwargs) 22 return res 23 else: 24 print('用户名或者密码错误,请重新输入!') 25 elif auth_type == 'qita': 26 print('不知道什么类型') 27 return wrapper 28 return auth_func 29 30 31 @auth(auth_type='filedb') 32 def index(): 33 print('欢迎来到淘宝首页') 34 35 36 @auth(auth_type='filedb') 37 def shopping_car(name): 38 print('%s的购物车里有%s%s' % (name, '苹果', '芒果')) 39 40 41 @auth(auth_type='filedb') 42 def order(): 43 pass 44 45 46 print('befor--->', current_dic) 47 index() 48 print('after-->', current_dic) 49 shopping_car('nihao')

浙公网安备 33010602011771号