给函数增加一个计时功能
def timer(func): # func = home
def inner(*args,**kwargs):
start_time = time.time()
ret = func(*args,**kwargs)
end_time = time.time()
print(f'此函数的执行效率为{end_time-start_time}')
return ret
return inner
@timer # home = timer(home)
def home(name,age):
time.sleep(3) # 模拟一下网络延迟以及代码的效率
print(name,age)
print(f'欢迎访问{name}主页')
home('李宽洋',18)
给装饰器传参数
def auth(x):
def auth2(func):
def inner(*args, **kwargs):
if login_status['status']:
ret = func()
return ret
if x == 'wechat':
username = input('请输入用户名:').strip()
password = input('请输入密码:').strip()
if username == '太白' and password == '123':
login_status['status'] = True
ret = func()
return ret
elif x == 'qq':
username = input('请输入用户名:').strip()
password = input('请输入密码:').strip()
if username == '太白' and password == '123':
login_status['status'] = True
ret = func()
return ret
return inner
return auth2
@auth('wechat')
def jitter():
print('记录美好生活')
@auth('qq')
def pipefish():
print('期待你的内涵神评论')