'''函数闭包带参数装饰器''' user_list = [ {'name':'alex', 'passwd':'123'}, {'name':'lhf', 'passwd':'123'}, {'name':'yuanhao', 'passwd':'123'}, {'name':'wupeiqi', 'passwd':'123'}, ] user_dic = {'name':None, 'login':False} def auth(auth_type='filedb'): def auth_func(func): def wrapper(*args, **kwargs): print('认证类型是',auth_type) if auth_type == 'filedb': if user_dic['name'] and user_dic['login']: res = func(*args, **kwargs) return res username = input('请输入用户名:').strip() passwd = input('请输入密码:').strip() for i in user_list: if username == i['name'] and passwd == i['passwd']: user_dic['name'] = username user_dic['login'] = True res = func(*args, **kwargs) return res else: print('用户名或密码错误') elif auth_type == 'ldap': print('不知道怎么玩') else: print('你这个认证无法识别') return wrapper return auth_func @auth(auth_type='filedb') # auth_func=auth(auth_type='filedb')-->@auth_func 附加了一个auth_type-->index=auth_func(index) def index(): print('欢迎登录系统') @auth(auth_type='ldap') def home(name): print('%s欢迎回到个人主页' % name) @auth(auth_type='bbb') def shopping_car(name): print('%s,您的购物清单里有%s,%s,%s' % (name, '篮球', '足球', '橄榄球')) index() home('wupeiqi') shopping_car('alex')