python_63_装饰器6
#decorator意思:1.装饰器 2.语法糖 import time user,passwd='qi','123' def auth(func): def wrappper(*args, **kwargs): 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 authentiction') return res #func(*args, **kwargs)#如果将以上三步换成本步,则打印不出from home,可以换成 return func(*args, **kwargs) else: exit('\033[31;1mInvalid username or password\033[0m') return wrappper def index():#首页(不需要登录) print('Welcome to index page!') @auth def home(auth_type='local'):#主页(需要登录) print('Welcome to home page!') return 'from home' @auth def bbs(auth_type='ldap'):#论坛页(不需要登录 ) print('Welcome to bbs page!') index() print(home())#调用home相当于调用wrapper() bbs()