双层语法糖
import time
def outer(func):
def get_time(*args, **kwargs):
start_time = time.time()
res = func(*args, **kwargs) # 只能够统计index函数的时间
end_time = time.time()
print('执行时间:%s' % (end_time - start_time))
return res
return get_time
def login_auth(func):
# func = index
def auth():
username = input('username:>>>').strip()
password = input('password:>>>').strip()
# 2. 比较用户名和密码
if username == 'jerry' and password == '123':
# 执行函数
print('登录成功')
func()
else:
print('用户名或者密码错误')
return auth
@login_auth # index=login_auth(get_time) # index=auth
@outer # get_time=outer(index)
def index():
time.sleep(3)
print('from index')
index() # auth()
![](https://i-blog.csdnimg.cn/blog_migrate/9929427a8bd88c15f553be2838395515.png)
三层语法糖
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')
index()
# 加载了outter3
# 加载了outter2
# 加载了outter1
# 执行了wrapper1
# 执行了wrapper2
# 执行了wrapper3
# from index
装饰器的修复技术(了解)
from functools import wraps
def outer(func):
@wraps(func) # 修复技术
def get_time():
start_time = time.time()
func() # 只能够统计index函数的时间
print(func) #<function index at 0x000001987321D120>
end_time = time.time()
print('执行时间:%s' % (end_time - start_time))
return get_time
@outer # index=outer(index)
def index():
time.sleep(3)
print('from index')
'''修复技术就是为了让装饰器伪装的更像'''
print(index) # <function outer.<locals>.get_time at 0x000001987327D480>
index()
有参装饰器(重要)
def outter(source_type):
# 'file', 1, 2, 3, 4, 5, 6,
# source_type = 'file'
def login_auth(func): # 参数个数只能有一个
def auth(*args, **kwargs): #
username = input('username:>>>').strip()
password = input('password:>>>').strip()
# 2. 比较用户名和密码
"""
1. 文件中获取用户名和密码
2. 从MySQL中获取用户名和密码
3. 从oracle中获取用户名和密码
4. 从postgresql中获取用户名和密码
"""
# print(a, b, c, d, e, f)
if source_type == 'file':
print('文件中获取用户名和密码')
elif source_type == 'mysql':
print('从MySQL中获取用户名和密码')
elif source_type == 'oracle':
print('从oracle中获取用户名和密码')
elif source_type == 'postgresql':
print('从postgresql中获取用户名和密码')
if username == 'jerry' and password == '123':
# 执行函数
print('登录成功')
func(source_type, *args, **kwargs)
else:
print('用户名或者密码错误')
return auth
return login_auth
@outter('file') # login_auth(home, file)
# @login_auth # login_auth(home, file)
def home():
pass
home('mysql')
三个练习题
请实现一个装饰器,把函数的返回值+100然后返回:
def outer1(*args1, **kwargs):
def outer(func):
def inner(*args, **kwargs):
set = func(*args, **kwargs)
return set + args1[0]
return inner
return outer
@outer1(100) # index=ouer(index)
def index():
print("aa")
return 500
print(index())
请实现一个装饰器,通过一次调用使函数重复执行5次
def outer(func):
def inner(*args, **kwargs):
count=0
while count<5:
set = func(*args, **kwargs)
count+=1
return inner
return inner
@outer
def index():
print("a")
index()
请实现一个装饰器每次调用函数时,将函数名字写入文件中
def outer1(*args1, **kwargs):
with open("aaa.txt", "a", encoding="utf-8") as f:
f.write(outer1.__name__ + "\n")
def outer(func):
def inner(*args, **kwargs):
with open("aaa.txt", "a", encoding="utf-8") as f:
f.write(inner.__name__ + "\n")
set = func(*args, **kwargs)
with open("aaa.txt", "a", encoding="utf-8") as f:
f.write(func.__name__ + "\n")
return set + args1[0]
return inner
return outer
@outer1(100) # index=ouer(index)
def index():
print("aa")
return 500
index()