认证功能装饰器

# _*_ coding: utf-8 _*_
import time

def auth(func):
# func = index原始的内存地址
# 相当于这里有个func = index
# print('welcome to index page')
# time.sleep(3)
def wrapper(*args,**kwargs):
inp_usr = input('please input your username: ').strip()
inp_pwd = input('please input your password: ').strip()
if inp_usr == 'ooc' and inp_pwd == '123':
print('login successful')
res=func(*args,**kwargs) # 没有参数,
# 在auth----wrapper中找到func=index
return res# index中无返回值 所以返回None
else:
print('username or password error')
return wrapper#返回wrapper的函数的内存地址

@auth #index = auth(index) index=wrapper
def index():
print('welcome to index page')
time.sleep(3)

res = index() #wrapper()
print(res) #无返回值所以返回None
原文地址:https://www.cnblogs.com/OutOfControl/p/9714830.html