Python笔记总结week5

时间:2021-07-10 15:19:46

Decorator:多层装饰器

#双层装饰器(用户登录,权限)
#多层: 调用从最外层到最内层函数,返回值则从最内到最外层函数 USER_INFO = {}
#USER_INFO['is_login'] = True
#USER_INFO['user_type'] = 2 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('user_type',None) == 2:
ret = func(*args, **kwargs)
return ret
else:
print("You are not allowed to do this")
return inner @check_login
@check_admin
def index():
print("index") index()

Packages:创建与运用