user_list = [ {'name': 'sb1', 'passwd': '123'}, {'name': 'sb2', 'passwd': '123'}, {'name': 'sb3', 'passwd': '123'}, {'name': 'sb4', 'passwd': '123'}]# 初始状态,用来保存登陆的用户,client_dic = {'username': None, 'login': False}# 添加新功能def auth(auth_type='filedb'): def auth_func(func): def wrapper(*args, **kwargs): print(auth_type) if auth_type == 'fildb': # 参数检查,判断是否有用户登录,如果有,不用验证,直接执行函数的功能 if client_dic['username'] and client_dic['login']: res = func(*args, **kwargs) return res # 输入用户名和密码 username = input('用户名:').strip() passwd = input('passwd:').strip() # 对比列表,检查用户名和密码是否正确 for user_dic in user_list: if username == user_dic['name'] and passwd == user_dic['passwd']: client_dic['username'] = user_dic['name'] client_dic['login'] = True res = func(*args, **kwargs) return res else: print('用户名或者密码错误!') elif auth_type == 'pass': print('不知道什么验证方式') res = func(*args, **kwargs) return res else: print('一脸蒙蔽的验证方式') res = func(*args, **kwargs) return res return wrapper return auth_func@auth(auth_type='filedb')def index(): print("欢迎来到主页")@auth(auth_type='user')def home(name): print("欢迎回家:%s" % name)@auth(auth_type='pass')def shoppping_car(): print('购物车里有[%s,%s,%s]' % ('奶茶', '妹妹', '娃娃'))print(client_dic)index()print(client_dic)home('root')