# -*- coding: utf-8 -*-
username = "liyuzhoupan"
password = "123"
def author(wrappreType):
print("wrappreType:", wrappreType)
def outterwrapper(func):
def wrapper(*args, **kwargs):
if wrappreType == "login1":
username1 = input("Username:").strip()
password1 = input("Password:").strip()
if username == username1 and password == password1:
print("/033[32;1m Login success!/033[0m")
return func(*args, **kwargs)
else:
exit("/033[31;1m login fail /033[0m")
elif wrappreType == "login2":
print("login2 login")
return func(*args, **kwargs)
else:
print("xia J8 che")
return wrapper
return outterwrapper
def hello_index():
print("welcome to index")
@author(wrappreType="login1") # 调用装饰器,根据条件wrappreType进行调用
def hello_home():
print("this is hello_home")
return "from home"
@author(wrappreType="login2")
def hello_bbs():
print("this is hello_index")
return "from bbs"
hello_index()
print(hello_home())
print(hello_bbs())
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/282012.html