#Author:donghuiya #decorator高级版 user,passwd='alex','abc123'#定义多个变量的写法 def auth(auth_type): print("auth_type",auth_type) def out_wrapper(func): def wrapper(*args,**kwargs): #print("wrapper func args:",*args,**kwargs) if auth_type=='local': username=input('username:').strip() password=input('passwpord:').strip() if user==username and passwd==password: ref=func(*args,**kwargs) print("\033[32;1mUser has passed authentication\033[0m") return ref #返回wrapper的返回结果,不然集丢失了home函数的return数据 else : exit('\033[31;1mInvaild username or password\033[0m') elif auth_type=='ldap': print("ldap。。。。。。。。。。。") return wrapper return out_wrapper def index(): print("welcome to index page") @auth(auth_type='local') def home(age,hobby): print("welcome to home page") return 'from home' @auth(auth_type='ldap') def bbp(): print("welcome to bbp page") #运行 print(home(22,'bhu'))#输出home的返回结果‘from home’ bbp()
装饰器高级版
最新推荐文章于 2024-11-14 10:09:32 发布