user_list = [
{'name': 'alex', 'passwd': 'alex123'},
{'name': 'xueyi', 'passwd': 'xueyi123'},
{'name': 'momo', 'passwd': 'momo123'}
]
current_user = {'name': None, 'login': False}
def auth(authType = 'filedb'):
def auth_login(func):
def login(*args, **kwargs):
if authType == 'filedb':
if current_user['name'] and current_user['passwd']:
res = func(*args, **kwargs)
return res
user_name = input('请输入用户名:')
passwd = input('请输入用户密码:')
for i, v in enumerate(user_list):
if user_name == v['name'] and passwd == v['passwd']:
current_user['name'] = user_name
current_user['passwd'] = passwd
res = func()
return res
else:
print('用户名或密码错误!')
elif authType == 'ldap':
print('不知道怎么玩')
else:
print('去死吧')
return login
return auth_login
@auth() # @auth() = auth(authType = 'filedb) = auth_login(index) 已经添加了一个authType参数 = index = login
def index():
print('欢迎来到京东')
@auth(authType = 'ldap')
def home(name):
print('%s 欢迎回家' % name)
index() # index() = login()
home('产品经理')
函数闭包带参数装饰器
最新推荐文章于 2022-06-28 11:43:51 发布