装饰器作用的完善

# 2.装饰器运用的例题,模拟网上商城的验证功能。
user_list=[
{'name':'a','passwd':'123'},
{'name':'b','passwd':'123'},
{'name':'c','passwd':'123'},
{'name':'d','passwd':'123'},
]
user_static={'username':"",'login':False}
def yanzheng(func):
def jiance(*args,**kwargs):
if user_static["username"] and user_static["login"]:
ret = func(*args, **kwargs)
return ret
username = input("请输入用户名:").strip()
userpasswd = input("请输入密码:").strip()
for user_dict in user_list:
if username==user_dict["name"] and userpasswd==user_dict["passwd"]:
print("验证通过")
user_static['username']=username
user_static['login' ]=True
ret=func(*args,**kwargs)
return ret
else:
print("输入用户名或密码有误,请重新输入")
return jiance
@yanzheng
def index(name):
print("欢迎【%s】来到XX购物网站" %(name))
@yanzheng
def buycar(name):
print("欢迎【%s】购买的【%s】" % (name,["汽水,面包,白菜,鲜奶"]))
def buy():
print("准备好结账吧")

index("张三")
buycar("张三")
posted @ 2019-04-09 11:23  菜鸟阿呆  阅读(168)  评论(0编辑  收藏  举报