python装饰器模块加载后的若干解释

本文章为私人文章

2019-05-10

代码:

def test(func):

    print('start decorate function')
    def decorate(*args):
        print('function name is ', func)
        print('params are ', args)

        result = func(args)
        return result
    return decorate

@test
def f1(one):
    print(one)

这个代码存储到register.py文件中,test是一个装饰器,它将被装饰的函数替换为其内部的decorate(*args)函数。

测试代码:

在交互中import register,输出结果:

>>>start decrorate function

这里的机制是,python在register模块导入时,发现了test装饰器以及test装饰了f1()函数,于是它开始执行test内部的打印语句,以及执行替换f1()为test内部的deorate()函数,注意这里进行了两步操作,打印和替换,换句话说就是(1)执行test内的公共部分,(2)执行函数替换操作。

要注意的是在第二部函数替换中,python进行了隐式的替换,它只是替换,但是没有执行这个decorate函数,真正要执行这个函数的时候,要等到调用f1()的时候,比如

>>>f1('hello")

输出结果为:

>>>function name is  
>>>params are  ('hello',)
>>>('hello',)

这里调用f1()的时候,python知道它之前已经被替换,于是转去执行那个替换f1()的函数,也就是decorate

 

2019-05-11

现在来看看带参数的装饰器的情况,这种情况与上面的有所不同

代码:

func_list = []

def func_regi(activate=True):
    print('hello world')
    def decorate(func):
        print('start decorate')
        if activate:
            func_list.append(func)
        else:
            func_list.append('0')
        return func
    return decorate

@func_regi(activate=True)
def f2():
    print('f2()')

可以看到,@func_regi后面带了参数,实际上func_regi变成了一个装饰器工厂函数,调用它将会返回一个装饰器,从而修饰被装饰的函数。

当导入模块时,import register(这个代码在register.py中),可以看到输出:

>>>hello world

>>>start decorate

很显然,跟上面第一种情况不一样,python在加载这样的模块时发现了f2()被func_regi修饰,于是调用了decorate()装饰器替换了f2(),与此同时,它也执行了decorate函数体。

 

你可能感兴趣的:(python装饰器模块加载后的若干解释)