发布于2019-08-05 19:33 阅读(1194) 评论(0) 点赞(5) 收藏(4)
def auth(argv):
def wrapper(func):
def inner(*args,**kwargs):
func(*args,**kwargs)
return inner
return wrapper
login_dic = {
"username": None,
"flag": False
}
def auth(argv): # argv == foo
def wrapper(func):
def inner(*args,**kwargs):
if login_dic["flag"]:
func(*args,**kwargs)
else:
if argv == "QQ":
user = input("username:")
pwd = input("password:")
if user == "alex" and pwd == "alex123": # qq
login_dic["flag"] = True
login_dic["username"] = user
func(*args,**kwargs)
else:
print("用户名或密码错误!")
elif argv == "微信":
user = input("username:")
pwd = input("password:")
if user == "1351101501" and pwd == "alex": # 微信
login_dic["flag"] = True
login_dic["username"] = user
func(*args, **kwargs)
else:
print("用户名或密码错误!")
elif argv == "抖音":
user = input("username:")
pwd = input("password:")
if user == "alexdsb" and pwd == "alex": # 抖音
login_dic["flag"] = True
login_dic["username"] = user
func(*args, **kwargs)
else:
print("用户名或密码错误!")
else:
user = input("username:")
pwd = input("password:")
if user == "alexdsb@dsb.com" and pwd == "alex": # 邮箱
login_dic["flag"] = True
login_dic["username"] = user
func(*args, **kwargs)
else:
print("用户名或密码错误!")
return inner
return wrapper
# 错误的案例
@auth # foo = auth(foo)
def foo():
print("这是一个被装饰的函数")
foo()
login_dic = {
"username": None,
"flag": False
}
# 正确的案例
msg = """
QQ
微信
抖音
邮箱
请输入您要选择登陆的app:
"""
chose = input(msg).upper()
def auth(argv):
def wrapper(func):
def inner(*args, **kwargs):
if login_dic["flag"]:
func(*args, **kwargs)
else:
if argv == "QQ":
print("欢迎登陆QQ")
user = input("username:")
pwd = input("password:")
if user == "alex" and pwd == "alex123": # qq
login_dic["flag"] = True
login_dic["username"] = user
func(*args, **kwargs)
else:
print("用户名或密码错误!")
elif argv == "微信":
print("欢迎登陆微信")
user = input("username:")
pwd = input("password:")
if user == "1351101501" and pwd == "alex": # 微信
login_dic["flag"] = True
login_dic["username"] = user
func(*args, **kwargs)
else:
print("用户名或密码错误!")
elif argv == "抖音":
print("来了,老弟!")
user = input("username:")
pwd = input("password:")
if user == "alexdsb" and pwd == "alex": # 抖音
login_dic["flag"] = True
login_dic["username"] = user
func(*args, **kwargs)
else:
print("用户名或密码错误!")
else:
print("欢迎登陆dab邮箱")
user = input("username:")
pwd = input("password:")
if user == "alexdsb@dsb.com" and pwd == "alex": # 邮箱
login_dic["flag"] = True
login_dic["username"] = user
func(*args, **kwargs)
else:
print("用户名或密码错误!")
return inner
return wrapper
@auth("QQ")
def foo():
print("这是一个被装饰的函数")
# wrapper = auth(chose)
# foo = wrapper(foo)
foo()
# @auth(chose) 相等于以下两行代码的解构
# wrapper = auth(chose)
# foo = wrapper(foo)
# 被装饰的函数正上方有多个装饰器,先执行离被装饰函数最近的装饰器
def wrapper1(func):
def inner1(*args, **kwargs):
print(1)
func(*args, **kwargs)
print(11)
return inner1
def wrapper2(func): # func == foo
def inner2(*args, **kwargs):
func(*args, **kwargs)
print(22)
return inner2
def wrapper3(func):
def inner3(*args, **kwargs):
print(3)
func(*args, **kwargs)
print(33)
return inner3
@wrapper1 # 1 11
@wrapper3 # 3 33
@wrapper2 # 8 22
def foo():
print(8)
foo = wrapper2(foo) # foo == inner2
foo = wrapper3(foo) # inner3 = wrapper3(inner2)
foo = wrapper1(foo) # inner1 = wrapper1(inner3)
foo() # inner1()
foo = wrapper3(foo) # foo == inner3
foo = wrapper2(foo) # foo = wrapper2(inner3) foo == inner2
foo = wrapper1(foo) # foo = wrapper1(inner2)
作者:vike
链接:https://www.pythonheidong.com/blog/article/7103/a9defef14100058198ec/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!