当前位置: 移动技术网 > IT编程>脚本编程>Python > python装饰器原理和用法总结

python装饰器原理和用法总结

2020年05月05日  | 移动技术网IT编程  | 我要评论

重生之篮球战场指挥家,长途汽车票查询时刻表 票价,ca1867

原理

装饰器本质也是一个函数, 只不过这个函数需要遵循以下规则:

  • 入参只能有一个,类型为函数。 被装饰的函数将入会被传入这个参数
  • 返回值是必须是一个函数, 届时被调用的时候实际上调用的是返回出来的这个函数,所以返回的函数入参通常是
(*args, **kwargs):

以满足所有函数需要

之后通过@语法糖即可装饰到任意函数上

简单装饰器例子

# 不带参数的装饰器
def pre_do_sth(func):
    def wrapper(*args, **kwargs):
        print("do sth before call one")
        func(*args, **kwargs)
    return wrapper
  
@pre_do_sth
def echo(msg):
    print(msg)
    
echo("hello world")

运行结果

do sth before call one
hello world

实际上调用的是 wrapper("hello world") --> echo("hello world")

带参数的装饰器例子(参数控制的是装饰器的行为)

只需要写一个返回 装饰器(入参只有一个, 返回值是一个函数)函数的函数

同样也能利用@语法糖

# 带参数的装饰器
def pre_do_sth_2(msg):
    def decorator(func):
        def wrapper(*args, **kwargs):
            print("do sth before call two, print:%s"%(msg))
            func(*args, **kwargs)
        return wrapper
    return decorator


@pre_do_sth_2("foo")
def echo(msg):
    print(msg)

echo("hello world")

实际上@后面并不是对pre_do_sth_2这个函数生效 而是对pre_do_sth_2的返回值生效

运行结果

do sth before call two, print:foo
hello world

多个装饰器调用顺序

先声明的装饰器先执行, 即在最外层

# 不带参数的装饰器
def pre_do_sth(func):
    def wrapper(*args, **kwargs):
        print("do sth before call one")
        func(*args, **kwargs)
    return wrapper

# 带参数的装饰器
def pre_do_sth_2(msg):
    def decorator(func):
        def wrapper(*args, **kwargs):
            print("do sth before call two, print:%s"%(msg))
            func(*args, **kwargs)
        return wrapper
    return decorator


@pre_do_sth
@pre_do_sth_2("foo")
def echo(msg):
    print(msg)

echo("hello world")

运行结果

do sth before call one
do sth before call two, print:foo
hello world

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网