#-*- coding;utf-8 -*-
user,passwd = 'alex','abc123'
def auth(auth_type):
print("auth func:",auth_type)
def outer_wrapper(func):
def wrapper(*args, **kwargs):
print("wrapper func args:", *args, **kwargs)
if auth_type == "local":
username = input("Username:").strip()
password = input("Password:").strip()
if user == username and passwd == password:
print("\033[32;1mUser has passed authentication\033[0m")
res = func(*args, **kwargs) # from home
print("---after authenticaion ")
return res
else:
exit("\033[31;1mInvalid username or password\033[0m")
elif auth_type == "ldap":
print("搞毛线ldap,不会。。。。")
return wrapper
return outer_wrapper
def index():
print("welcome to index page")
@auth(auth_type="local") # home = wrapper()
def home():
print("welcome to home page")
return "from home"
@auth(auth_type="ldap")
def bbs():
print("welcome to bbs page")
index()
print(home()) #wrapper()
bbs()
auth func: local
auth func: ldap
welcome to index page
wrapper func args:
Username:Alex
Password:abc123
None
wrapper func args:
搞毛线ldap,不会。。。。
装饰器总结:装饰器本质上是一个函数,通过这个函数去装饰另一个函数,当调用函数home()时,实际上调用了auth(auth_type),再定义高阶函数outer_wrapper将home的地址作为形参,再通过嵌套函数wrapper执行函数home()