翼度科技»论坛 编程开发 python 查看内容

装饰器2

10

主题

10

帖子

30

积分

新手上路

Rank: 1

积分
30
装饰器

装饰器的简易版本
  1. import time
  2. def index():
  3.     time.sleep(3)
  4.     print('from index')
  5.    
  6.    
  7. def home():
  8.     print('from home')
  9.    
  10. def func():
  11.     print('from func')
  12.    
  13.    
  14. def outer(func_name):
  15.     # func_name = index
  16.     def get_time():
  17.         # 1. 函数执行之前打一个时间点
  18.         start_time = time.time()
  19.         func_name() # index() home()
  20.         # 2. 在函数执行之后,在打一个时间点
  21.         end_time = time.time()
  22.         print('总共执行了:%s' % (end_time - start_time))
  23.         return get_time
  24. # get_time(index)
  25. # get_time(home)
  26. index=outer(index)  # res=get_time
  27. index() # get_time()
复制代码
装饰器解决参数问题
  1. import time
  2. def index():
  3.     time.sleep(3)
  4.     print('from index')
  5.    
  6.    
  7. def home(name, u, a):
  8.     print('from home')
  9.     return 'from home'
  10. def func():
  11.     print('from func')
  12.    
  13.    
  14. def outer(func_name):
  15.     # func_name = index
  16.     def get_time(*args, **kwargs): args=() kwargs={'username':jerry, 'age':18}
  17.         # 1. 函数执行之前打一个时间点
  18.         
  19.         start_time = time.time()
  20.         func_name(*args, **kwargs) # index() home()
  21.         res=func_name(name, username='jerry', age=18) # index() home()
  22.         # 2. 在函数执行之后,在打一个时间点
  23.         end_time = time.time()
  24.         print('总共执行了:%s' % (end_time - start_time))
  25.         return res
  26.         return get_time
  27. # get_time(index)
  28. # get_time(home)
  29. index=outer(index)  # res=get_time
  30. index() # get_time()
  31. home=outer(home)  # res=get_time
  32. res=home('kevin', username='jerry', age=18) # get_time()
  33. print(res) #  None
  34. """认证登录的装饰器,当你访问函数的时候,必须登录之后才能够访问!"""
复制代码
装饰器的固定模板
  1. def index():
  2.     pass
  3. def outer(func_name):
  4.     def inner(*args, **kwargs):
  5.         '''添加一些函数执行之前的功能'''
  6.         res=func_name(*args, **kwargs) # 这个就是执行的真正的函数
  7.         '''添加一些函数执行之后的功能'''
  8.         return res
  9.    
  10.     return inner
  11. '''装饰器本质上还是函数!'''
  12. @outer
  13. @outer # index=outer(index)
  14. def index():
  15.     pass
  16. # index=outer(index)
  17. index()
  18. """
  19. 1. 语法糖的书写规范
  20.         @装饰器名字
  21.         把语法糖紧贴着写在函数的头部
  22. 2. 装饰器原理:
  23.         把被装饰对象当成函数的参数传递给装饰器的形参
  24. """
复制代码
双层语法糖
  1. import time
  2. def outer(func):
  3.     def get_time(*args, **kwargs):
  4.         start_time = time.time()
  5.         res = func(*args, **kwargs)  # 只能够统计index函数的时间
  6.         end_time = time.time()
  7.         print('执行时间:%s' % (end_time - start_time))
  8.         return res
  9.     return get_time
  10. def login_auth(func):
  11.     # func = index
  12.     def auth():
  13.         username = input('username:>>>').strip()
  14.         password = input('password:>>>').strip()
  15.         # 2. 比较用户名和密码
  16.         if username == 'jerry' and password == '123':
  17.             # 执行函数
  18.             print('登录成功')
  19.             func()
  20.         else:
  21.             print('用户名或者密码错误')
  22.     return auth
  23. @login_auth # index=login_auth(get_time) # index=auth
  24. @outer      # get_time=outer(index)
  25. def index():
  26.     time.sleep(3)
  27.     print('from index')
  28. index() # auth()
复制代码
三层语法糖(多层)
  1. # 判断七句print执行顺序
  2. def outter1(func1):
  3.     print('加载了outter1')
  4.     def wrapper1(*args, **kwargs):
  5.         print('执行了wrapper1')
  6.         res1 = func1(*args, **kwargs)
  7.         return res1
  8.     return wrapper1
  9. def outter2(func2):
  10.     print('加载了outter2')
  11.     def wrapper2(*args, **kwargs):
  12.         print('执行了wrapper2')
  13.         res2 = func2(*args, **kwargs)
  14.         return res2
  15.     return wrapper2
  16. def outter3(func3):
  17.     print('加载了outter3')
  18.     def wrapper3(*args, **kwargs):
  19.         print('执行了wrapper3')
  20.         res3 = func3(*args, **kwargs)
  21.         return res3
  22.     return wrapper3
  23. @outter1
  24. @outter2
  25. @outter3
  26. def index():
  27.     print('from index')
  28. index()
复制代码
装饰器的修复技术(了解)
  1. import time
  2. from functools import wraps
  3. def outer(func):
  4.     @wraps(func) # 修复技术
  5.     def get_time():
  6.         start_time = time.time()
  7.         func()  # 只能够统计index函数的时间
  8.         end_time = time.time()
  9.         print('执行时间:%s' % (end_time - start_time))
  10.     return get_time
  11. # @outer  # index=outer(index)
  12. def index():
  13.     print('from index')
  14. '''修复技术就是为了让装饰器伪装的更像'''
  15. # index()
  16. # print(index) # <function index at 0x000002F69849A940>
  17. # print(index) # <function index at 0x000002F69849A940>
  18. # help(index)
  19. @outer
  20. def home():
  21.     '''这是home函数'''
  22. help(home)
复制代码
有参装饰器(重要)
  1. def outter(source_type, *args1, **kwargs1):
  2.     # 'file', 1, 2, 3, 4, 5, 6,
  3.     # source_type = 'file'
  4.     def login_auth(func):  # 参数个数只能有一个
  5.         def auth(*args, **kwargs): #
  6.             username = input('username:>>>').strip()
  7.             password = input('password:>>>').strip()
  8.             # 2. 比较用户名和密码
  9.             """
  10.                 1. 文件中获取用户名和密码
  11.                 2. 从MySQL中获取用户名和密码
  12.                 3. 从oracle中获取用户名和密码
  13.                 4. 从postgresql中获取用户名和密码
  14.             """
  15.             # print(a, b, c, d, e, f)
  16.             if source_type == 'file':
  17.                 print('文件中获取用户名和密码')
  18.             elif source_type == 'mysql':
  19.                 print('从MySQL中获取用户名和密码')
  20.             elif source_type == 'oracle':
  21.                 print('从oracle中获取用户名和密码')
  22.             elif source_type == 'postgresql':
  23.                 print('从postgresql中获取用户名和密码')
  24.             if username == 'jerry' and password == '123':
  25.                 # 执行函数
  26.                 print('登录成功')
  27.                 func(source_type, *args, **kwargs)
  28.             else:
  29.                 print('用户名或者密码错误')
  30.         return auth
  31.     return login_auth
  32. @outter('file', 1, 2, 3, 4, 5, 6,) # login_auth(home, file)
  33. # @login_auth # login_auth(home, file)
  34. def home():
  35.     pass
  36. home('mysql')
复制代码
来源:https://www.cnblogs.com/zhangfanshixiaobai/p/17694426.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具