1 user_list=[ 2 { 'name':'alex','passwd':'123'}, 3 { 'name':'linhaifeng','passwd':'123'}, 4 { 'name':'wupeiqi','passwd':'123'}, 5 { 'name':'yuanhao','passwd':'123'}, 6 ] 7 current_dic={ 'username':None,'login':False} 8 9 10 def auth_func(func):11 def wrapper(*args,**kwargs):12 if current_dic['username'] and current_dic['login']:13 res = func(*args, **kwargs)14 return res15 username=input('用户名:').strip()16 passwd=input('密码:').strip()17 for user_dic in user_list:18 if username == user_dic['name'] and passwd == user_dic['passwd']:19 current_dic['username']=username20 current_dic['login']=True21 res = func(*args, **kwargs)22 return res23 else:24 print('用户名或者密码错误')25 26 return wrapper27 28 @auth_func29 def index():30 print('欢迎来到京东主页')31 32 @auth_func33 def home(name):34 print('欢迎回家%s' %name)35 36 @auth_func37 def shopping_car(name):38 print('%s的购物车里有[%s,%s,%s]' %(name,'奶茶','妹妹','娃娃'))39 40 print('before-->',current_dic)41 index()42 print('after--->',current_dic)43 shopping_car(current_dic["username"])44 home('产品经理')45 # shopping_car('产品经理')
1 before--> { 'username': None, 'login': False}2 用户名:alex3 密码:1234 欢迎来到京东主页5 after---> { 'username': 'alex', 'login': True}6 欢迎回家产品经理7 alex的购物车里有[奶茶,妹妹,娃娃]