标签:
1. Python视频
B. 装饰器
多层装饰器
USER_INFO = {} def check_login(func): def inner(*args, **kwargs): if USER_INFO.get(‘is_login‘, None): ret = func(*args, **kwargs) return ret else: print(‘please login‘) return inner def check_admin(func): def inner(*args, **kwargs): if USER_INFO.get(‘type‘, None) == 2: ret = func(*args, **kwargs) return ret else: print(‘no permission‘) return inner @check_login @check_admin def index(): # manager print(‘Index‘) def home(): # user print(‘home‘) def login(): user = input(‘input username\n>>>‘) pwd = input(‘input password\n>>>‘) if user == ‘admin‘ and pwd == ‘admin‘: USER_INFO[‘is_login‘] = True USER_INFO[‘type‘] = 2 else: if user == ‘wayne‘ and pwd == ‘phuck‘: USER_INFO[‘is_login‘] = True USER_INFO[‘type‘] = 1 def main(): while True: inp = input(‘1.login 2.information 3.management\n>>>‘) if inp == ‘1‘: login() elif inp == ‘2‘: home() elif inp == ‘3‘: index() main()
标签:
原文地址:http://www.cnblogs.com/wnzhong/p/5883766.html