【相关学习推荐:python教程】
# 不带参数的装饰器def deco_test(func): def wrapper(*args, **kwargs): print("before function") f = func(*args, **kwargs) print("after function") return f return wrapperdef do_something(a,b,c): print(a) time.sleep(1) print(b) time.sleep(1) print(c) return aif __name__ == '__main__': # 不用@ f = deco_test(do_something)("1","2","3")
输出:
before function 1 2 3 after function
个人理解:
相当于在 do_something
函数外面套了两个输出:before function
和 after function
。
# 不带参数的装饰器def deco_test(func): def wrapper(*args, **kwargs): print("before function") f = func(*args, **kwargs) print("after function") return f return wrapper @deco_testdef do_something(a,b,c): print(a) time.sleep(1) print(b) time.sleep(1) print(c) return aif __name__ == '__main__': # 使用@ f = do_something("1","2","3")
输出:
before function 1 2 3 after function
个人理解:
相当于执行 do_something
函数的时候,因为有 @
的原因,已经知道有一层装饰器 deco_test
,所以不需要再单独写 deco_test(do_something)
了。
# 带参数的装饰器def logging(level): def wrapper(func): def inner_wrapper(*args, **kwargs): print("[{level}]: enter function {func}()".format(level=level, func=func.__name__)) f = func(*args, **kwargs) print("after function: [{level}]: enter function {func}()".format(level=level, func=func.__name__)) return f return inner_wrapper return wrapper @logging(level="debug")def do_something(a,b,c): print(a) time.sleep(1) print(b) time.sleep(1) print(c) return aif __name__ == '__main__': # 使用@ f = do_something("1","2","3")
输出:
[debug]: enter function do_something() 1 2 3 after function: [debug]: enter function do_something()
个人理解:
装饰器带了一个参数 level = "debug"
。
最外层的函数 logging()
接受参数并将它们作用在内部的装饰器函数上面。内层的函数 wrapper()
接受一个函数作为参数,然后在函数上面放置一个装饰器。这里的关键点是装饰器是可以使用传递给 logging()
的参数的。
# 类装饰器class deco_cls(object): def __init__(self, func): self._func = func def __call__(self, *args, **kwargs): print("class decorator before function") f = self._func(*args, **kwargs) print("class decorator after function") return f @deco_clsdef do_something(a,b,c): print(a) time.sleep(1) print(b) time.sleep(1) print(c) return aif __name__ == '__main__': # 使用@ f = do_something("1","2","3")
输出:
class decorator before function 1 2 3 class decorator after function
个人理解:
使用一个装饰器去包装函数,返回一个可调用的实例。 因此定义了一个类装饰器。
# 不带参数的装饰器def deco_test(func): def wrapper(*args, **kwargs): print("before function") f = func(*args, **kwargs) print("after function") return f return wrapper# 带参数的装饰器def logging(level): def wrapper(func): def inner_wrapper(*args, **kwargs): print("[{level}]: enter function {func}()".format(level=level, func=func.__name__)) f = func(*args, **kwargs) print("after function: [{level}]: enter function {func}()".format(level=level, func=func.__name__)) return f return inner_wrapper return wrapper @logging(level="debug")@deco_testdef do_something(a,b,c): print(a) time.sleep(1) print(b) time.sleep(1) print(c) return aif __name__ == '__main__': # 使用@ f = do_something("1","2","3")
输出:
[debug]: enter function wrapper() before function 1 2 3 after function after function: [debug]: enter function wrapper()
个人理解:
在函数 do_something()
外面先套一层 deco_test()
装饰器,再在最外面套一层 logging()
装饰器。
想了解更多编程学习,敬请关注php培训栏目!
以上是聊聊Python装饰器的详细内容。更多信息请关注PHP中文网其他相关文章!