day14 装饰器模拟验证附加功能

 user_list=[
{'name':'alex','passwd':''},
{'name':'linhaifeng','passwd':''},
{'name':'wupeiqi','passwd':''},
{'name':'yuanhao','passwd':''},
]
current_dic={'username':None,'login':False} def auth_func(func):
def wrapper(*args,**kwargs):
if current_dic['username'] and current_dic['login']:
res = func(*args, **kwargs)
return res
username=input('用户名:').strip()
passwd=input('密码:').strip()
for user_dic in user_list:
if username == user_dic['name'] and passwd == user_dic['passwd']:
current_dic['username']=username
current_dic['login']=True
res = func(*args, **kwargs)
return res
else:
print('用户名或者密码错误') return wrapper @auth_func
def index():
print('欢迎来到京东主页') @auth_func
def home(name):
print('欢迎回家%s' %name) @auth_func
def shopping_car(name):
print('%s的购物车里有[%s,%s,%s]' %(name,'奶茶','妹妹','娃娃')) print('before-->',current_dic)
index()
print('after--->',current_dic)
shopping_car(current_dic["username"])
home('产品经理')
# shopping_car('产品经理')
 before--> {'username': None, 'login': False}
用户名:alex
密码:123
欢迎来到京东主页
after---> {'username': 'alex', 'login': True}
欢迎回家产品经理
alex的购物车里有[奶茶,妹妹,娃娃]
上一篇:6-03使用SQL语句一次型向表中插入多行数据


下一篇:获取linux目录下最新的文件