ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Python函数解释器进阶(三)

2021-11-17 22:33:11  阅读:107  来源: 互联网

标签:index 解释器 outer 进阶 Python time print login def


一.装饰器的简易版本

 1 # 装饰器简易版本
 2 def index():
 3     time.sleep(3)
 4     print('哈哈哈哈')
 5 import time
 6 def outer(func):
 7     def get_time():
 8         start_time = time.time()
 9         func()
10         end_time = time.time()
11         print('函数运行了%s' % (end_time - start_time))
12     return get_time
13 index = outer(index)
14 index()

 

 

二.解决参数问题

 1 # 解决参数问题
 2 import time
 3 def index():
 4     time.sleep(3)
 5     print('哈哈哈哈')
 6 def login(name):
 7     time.sleep(1)
 8     print('用户%s输入成功' % name)
 9 def outer(func):
10     def get_time(*args, **kwargs):
11         start_time = time.time()
12         func(*args, **kwargs)
13         end_time = time.time()
14         print('函数运行时间%s' % (end_time - start_time))
15     return get_time
16 index = outer(index)
17 index()
18 login = outer(login)
19 login('jack')

 

 

三.解决函数返回值问题

 1 import time
 2 def login(name):
 3     time.sleep(1)
 4     print('用户%s输入成功' % name)
 5     return 'from login'
 6 def outer(func):  # func指向的是函数名login
 7     # func = login
 8     def get_time(*args,**kwargs):
 9         start_time = time.time()
10         res = func(*args,**kwargs)  # 接收被装饰函数的返回值
11         end_time = time.time()
12         print('函数运行时间:%s' % (end_time - start_time))
13         return res  # 执行完get_time之后返回被装饰函数执行之后的返回值
14     return get_time  # 将get_time函数名返回出去
15 login = outer(login)
16 res1 = login('jason')
17 print(res1)

 

 

四.认证装饰器

 1 # 定义一个用于记录用户数据是否登录的数据
 2 is_login = {'is_login': False}
 3 import time
 4 def index():
 5     time.sleep(1)
 6     print('恭喜你中奖了')
 7 def study():
 8     time.sleep(2)
 9     print('学习不是一天完成的事情,要坚持不懈')
10 def register():
11     time.sleep(3)
12     print('注册功能')
13 def login_auth(func):
14     def auth(*args, **kwargs):
15         # 判断用户是否已经登录
16         if is_login.get('is_login'):
17             res = func(*args, **kwargs)
18             # 登录的话直接运行函数
19             return res
20         username = input('请输入用户名').strip()
21         password = input('请输入密码').strip()
22         # 判断登录账号是否正确
23         if username == 'jason' and password == '123':
24             # 正常执行函数
25             res = func()
26             # 将记录用户状态的数据修改
27             is_login['is_login'] = True
28             return res
29         else:
30             print('用户名或者密码错误')
31     return auth
32 index=login_auth(index)
33 index()
34 study=login_auth(study)
35 study()
36 register=login_auth(register)
37 register()

先登录账号,账号正确的话后面两个函数自动执行

 

 

五.装饰器固定模板

这个模板,写代码时可以直接使用

def outer(func):
    def inner(*args, **kwargs):
        print('执行函数之前可以添加的额外功能')
        res = func(*args, **kwargs)  # 执行被装饰的函数
        print('执行函数之后可以添加的额外功能')
        return res  # 将被装饰函数执行之后的返回值返回
    return inner

六.装饰器语法糖

 1 def outer(func):
 2     def inner(*args, **kwargs):
 3         print('执行函数之前可以添加的额外功能')
 4         res = func(*args, **kwargs)  # 执行被装饰的函数
 5         print('执行函数之后可以添加的额外功能')
 6         return res  # 将被装饰函数执行之后的返回值返回
 7     return inner
 8 @outer  # index = outer(index)
 9 def index(*args, **kwargs):
10     print('from index')
11 @outer  # home = outer(home)
12 def home():
13     print('from home')

装饰器语法糖书写规范

语法糖必须紧贴在被装饰函数的上方

装饰器语法糖内部原理

会自动将下面紧贴着的被装饰对象名字当做参数传给装饰器函数调用

七.装饰器修复技术

 1 from functools import wraps
 2 def outer(func):
 3     @wraps(func)  # 修复技术就是为了让被装饰对象更加不容易被察觉装饰了
 4     def inner(*args, **kwargs):
 5         print('执行函数之前可以添加的额外功能')
 6         res = func(*args, **kwargs)  # 执行被装饰的函数
 7         print('执行函数之后可以添加的额外功能')
 8         return res  # 将被装饰函数执行之后的返回值返回
 9     return inner
10 
11 
12 @outer  # index = outer(index)
13 def index():
14     print('from index')
15 print(index)
16 help(index)
17 
18 def home():
19     """这是一个home函数"""
20     print('from home')
21 # help(index)
22 # help(home)
23 # print(index)
24 # help(len)

八.有参装饰器

 1 def outer(source_data):
 2     # source_data = 'file'
 3     def login_auth(func):
 4         def auth(*args,**kwargs):
 5             # 2.校验用户名和密码是否正确
 6             # 数据的校验方式可以切换多种
 7             if source_data == 'file':
 8                 # 从文件中获取用户数据并比对
 9                 print('file文件获取')
10             elif source_data == 'MySQL':
11                 # 从MySQL数据库中获取数据比对
12                 print('MySQL数据库获取')
13             elif source_data == 'postgreSQL':
14                 # 从postgreSQL数据库中获取数据对比
15                 print('postgreSQL数据库获取')
16             else:
17                 print('用户名或密码错误 无法执行函数')
18         return auth
19     return login_auth
20 
21 @outer('file')
22 def index():
23     print('from index')
24 @outer('MySQL')
25 def home():
26     print('from home')
27 
28 index()
29 home()

 

标签:index,解释器,outer,进阶,Python,time,print,login,def
来源: https://www.cnblogs.com/liuwang999/p/15569849.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有