Python 装饰器“高级”使用

简介: 本文聚焦两个有意思的点1. 无参和有参装饰器。 @deco vs @deco(arg1,arg2)。2. 多层装饰器场景。

本文聚焦两个有意思的点

  1. 无参和有参装饰器。 @deco vs @deco(arg1,arg2)。
  2. 多层装饰器场景。

无参和有参装饰器

大部分文章,都会学习到无参有参装饰器写法。这里不赘述,直接上兼容括号和无括号注解 (无参 vs 有参),高级+灵活。

# 兼容有括号和无括号装饰器
def log3(func=None, /, *, text=None):
    """
    @log 装饰器(兼容有参和无参)
    三层嵌套.
    === f = log('text')(f) , f() 实际是 wrapper()
    :param text:
    :return:
    """
    def decorator(f):  # =~ 上面的 log 方法
        @functools.wraps(f)
        def wrapper(*args, **kw):
            t0 = time.time()
            print('[%s] start call %s at %s' % (text, f.__name__,
                                                time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(t0))))
            # 调用目标方法本身
            ret = f(*args, **kw)
            t1 = time.time()
            print('[%s] end call %s at %s, cost time: %s s' % (text, f.__name__,
                                                               time.strftime(
                                                                   '%Y-%m-%d %H:%M:%S', time.localtime(t1)),
                                                               t1-t0))
            return ret
        return wrapper
    
    # 判断 func 是否是函数
    if inspect.isfunction(func):
        # 无括号
        print('deco nopars')
        return decorator(func)  # 记忆诀窍, 无括号, 要补上
    return decorator
@log3
def tarFun3_nopar():
    print("tar Fun3 executing - nopar")
@log3(text='log333333')
def tarFun3_haspar():
    print("tar Fun3 executing - haspar")
# tarFun3_nopar()
tarFun3_haspar()

要点:

装饰器代码里三层函数。里面判断是否是无参调用还是调用,准确的说是不带括号调用还是带括号调用,根据不同是否方式,返回对应的函数。


分隔符 /,* 含义:

  • / , 分割符号,前面表示只能是位置参数。这就限制了 func 参数只能位置参数传入,而不能通过 func=x 传入。
  • , 后面的参数只能是k-v传参。


有这两个分隔符参数限制,可以避免调用传参导致的错误。

多层装饰器场景

看例子

def deco1(func):
    print("deco1")
    def deco1_wrapper(*args, **kwargs):
        print("deco1 wrapper", func)
        return func(*args, **kwargs)
    return deco1_wrapper
def deco2(func):
    print("deco2")
    def deco2_wrapper(*args, **kwargs):
        print("deco2 wrapper", func)
        return func(*args, **kwargs)
    return deco2_wrapper
@deco1
@deco2
def mult_deco():
    print("mult_deco")
mult_deco()
# ---
deco2  -- 解释执行期间打印
deco1  -- 解释执行期间打印
deco1 wrapper <function deco2.<locals>.deco2_wrapper at 0x000002A514699C60>
deco2 wrapper <function mult_deco at 0x000002A514699BC0>
mult_deco

解释期间,注解由下到上解释(执行)。距离目标方法最近的注解先被解释执行,可以理解为由内而外。


要点:类似洋葱

  1. 解释期间,由内而外
  2. 执行期间,由外而内

剥洋葱 @functools.wrap

另外,例子中发现 deco1 装饰器里 func 打印字符是 deco2_wrapper 字样。很容易理解,根据洋葱定律,外层包裹的是内层的,故 deco1 包裹的自然是 deco2 deco2_wrapper 方法。


那该怎么保留或获取原始被包裹方法的信息呢?


来,让我们剥洋葱!


这就需要借助工具:@functools.wrap

import functools
def deco1(func):
    print("deco1")
    @functools.wraps(func)  # 如果我最外层使用, 可以不剥, 剥是为了将原始函数信息暴漏给更外层的
    def deco1_wrapper(*args, **kwargs):
        print("deco1 wrapper", func)  # 这里 func 由于里层装饰器剥了一遍洋葱, 故而这里拿到的是原始函数 mult_deco 信息
        return func(*args, **kwargs)
    return deco1_wrapper
def deco2(func):
    print("deco2")
    @functools.wraps(func)  # 虽然返回的deco2_wrapper, 但函数信息却是剥过洋葱后的, 即原始函数 mult_deco
    def deco2_wrapper(*args, **kwargs):
        print("deco2 wrapper", func)
        return func(*args, **kwargs)
    return deco2_wrapper
@deco1
@deco2
def mult_deco():
    print("mult_deco")
mult_deco()
deco2
deco1
deco1 wrapper <function mult_deco at 0x0000023387C19C60>
deco2 wrapper <function mult_deco at 0x0000023387C19BC0>
mult_deco


@functools.wrap 详细原理,这里不深究了,只附带介绍下它的现象。


要点:自定义装饰器推荐必带 @functools.wrap 装饰器。


作者:Nisus

链接:https://juejin.cn/post/7398520872179843072

相关文章
|
1月前
|
测试技术 Python
Python装饰器:为你的代码施展“魔法”
Python装饰器:为你的代码施展“魔法”
235 100
|
2月前
|
设计模式 缓存 监控
Python装饰器:优雅增强函数功能
Python装饰器:优雅增强函数功能
267 101
|
1月前
|
缓存 Python
Python装饰器:为你的代码施展“魔法
Python装饰器:为你的代码施展“魔法
153 88
|
2月前
|
缓存 测试技术 Python
Python装饰器:优雅地增强函数功能
Python装饰器:优雅地增强函数功能
206 99
|
2月前
|
存储 缓存 测试技术
Python装饰器:优雅地增强函数功能
Python装饰器:优雅地增强函数功能
185 98
|
2月前
|
缓存 Python
Python中的装饰器:优雅地增强函数功能
Python中的装饰器:优雅地增强函数功能
|
2月前
|
存储 缓存 测试技术
理解Python装饰器:简化代码的强大工具
理解Python装饰器:简化代码的强大工具
|
3月前
|
程序员 测试技术 开发者
Python装饰器:简化代码的强大工具
Python装饰器:简化代码的强大工具
210 92
|
2月前
|
缓存 测试技术 Python
解锁Python超能力:深入理解装饰器
解锁Python超能力:深入理解装饰器
127 2

推荐镜像

更多