# coding=utf-8 # 高阶函数 + 嵌套函数=》装饰器
#这个是一个为home页面和bbs论坛页面添加登录认证的一个装饰器 user = "stanlong" passwd = "abc123" def auth(auth_type): def outer_wrapper(func): def wrapper(*args, **kwargs): print("wrapper func args:",*args, **kwargs) if auth_type == "local": username = input("input your username:").strip() password = input("input your password:").strip() if user == username and passwd == password: res = func(*args, **kwargs) print("--------------after login----------------") return res else: exit("wrong username or password") elif auth_type == "ldap": print("ldap认证方式") return wrapper return outer_wrapper def index(): print("welcome to index") @auth(auth_type = "local") def home(): print("wlcome to home") return "from home" @auth(auth_type = "ldap") def bbs(): print("welcome to bbs") index() home() bbs()