大家知道,装饰器可以给对象动态地添加行为。装饰器的两大应用场景是:
1 权限验证
2 添加日志
当然装饰器能应用的场景要远多于2种,下面分别用代码实现权限验证和日志:
1 权限验证
from functools import wraps
def login_check(func):
'''
Decorator
check whether user is logged in
:param func:
:return:
'''
@wraps(func)
def wrapper(*args, **kwargs):
if args[0].status != 'login':
# raise Exception('please log in first')
print('please log in first')
else:
return func(*args, **kwargs)
return wrapper
class Visitor(object):
def __init__(self, status):
self.status = status
# decorate this method
@login_check
def access_2_resources(self):
print('start downloading')
if __name__ == '__main__':
visitor = Visitor('login')
visitor.access_2_resources()
print('*'*100)
visitor.status = 'not login'
visitor.access_2_resources()
上述代码实现了login_check装饰器,装饰了access_2_resources方法,即调用这个方法的用户都必须进行登录验证代码结果如此
start downloading
****************************************************************************************************
please log in first
2 添加日志
from functools import wraps
def with_log(func):
@wraps(func)
def wrapper(*args, **kwargs):
print(func.__name__ + ' was called ' + '\nparmas: %s, %s' % (str(args), str(kwargs)))
return func(*args, **kwargs)
return wrapper
class Add(object):
def __init__(self):
self.name = 'add'
@with_log
def a_b(self, a, b):
return a + b
if __name__ == '__main__':
add = Add()
c = add.a_b(10, 23)
print(c)
with_log是一个添加日志的装饰器,当他装饰的函数被调用时,就会记录日志,日志内容包括函数名,和函数参数
结果如下
a_b was called
parmas: (<__main__.Add object at 0x0000021C7811C080>, 10, 23), {}
33