装饰器验证接口响应时长
from time import time def print_execute_time(func): def wrapper(*args, **kwargs): start = time() func_return = func(*args, **kwargs) end = time() print('函数:{name},执行时间:{time}'.format(name=func.__name__,time=end-start)) return func_return return wrapper @print_execute_time def test1(): for i in range(100): pass if __name__ == '__main__': test1()
装饰器验证权限
def is_superuser(fun): def wrapped(name,*args,**kwargs): if name == '张三': return fun() else: return False return wrapped # 调用装饰器:@+装饰器名 @is_superuser def hello(): return "hello world" if __name__ == '__main__': print(hello('张三1'))