首页 > 代码库 > 2016/09/18

2016/09/18

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 innerdef 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_admindef 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‘] = 1def 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()

 

2016/09/18