‘‘‘
装饰器:
不修改源代码,不修改其调用方法
‘‘‘
import getpass
import time
usr, pwd = "xiaobai", "111111"
#装饰器
def auth(auth_type):
def outter_wrapper(func):
def wrapper(*args, **kwargs):
if auth_type == "local":
username = input("usrname:").strip()
password = getpass.getpass("password:").strip()
if usr == username and pwd == password:
print("auth has passed!")
res = func(*args, **kwargs)
print("----after authentication")
return res
else:
exit("Invalid username or password!")
elif auth_type == "ladap":
print("不会。。。。。")
return wrapper
return outter_wrapper
#源代码
def index():
print("Welcome to the index page!")
@auth(auth_type="local")
def home():
print("Welcome to the home page!")
return "from home."
@auth(auth_type="ladap") # bbs = auth(auth_type="local")
def bbs():
print("Welcome to the bbs page!")
# index()
print(home())
# bbs()