Python中的各种装饰器详解
admin
2023-07-31 02:14:40
0

Python装饰器,分两部分,一是装饰器本身的定义,一是被装饰器对象的定义。

一、函数式装饰器:装饰器本身是一个函数。

1.装饰函数:被装饰对象是一个函数

[1]装饰器无参数:

a.被装饰对象无参数:

复制代码 代码如下:
>>> def test(func):
    def _test():
        print \’Call the function %s().\’%func.func_name
        return func()
    return _test

>>> @test
def say():return \’hello world\’

>>> say()
Call the function say().
\’hello world\’
>>>

b.被装饰对象有参数:

复制代码 代码如下:
>>> def test(func):
    def _test(*args,**kw):
        print \’Call the function %s().\’%func.func_name
        return func(*args,**kw)
    return _test

>>> @test
def left(Str,Len):
    #The parameters of _test can be \'(Str,Len)\’ in this case.
    return Str[:Len]

>>> left(\’hello world\’,5)
Call the function left().
\’hello\’
>>>

[2]装饰器有参数:

a.被装饰对象无参数:

复制代码 代码如下:
>>> def test(printResult=False):
    def _test(func):
        def __test():
            print \’Call the function %s().\’%func.func_name
            if printResult:
                print func()
            else:
                return func()
        return __test
    return _test

>>> @test(True)
def say():return \’hello world\’

>>> say()
Call the function say().
hello world
>>> @test(False)
def say():return \’hello world\’

>>> say()
Call the function say().
\’hello world\’
>>> @test()
def say():return \’hello world\’

>>> say()
Call the function say().
\’hello world\’
>>> @test
def say():return \’hello world\’

>>> say()

Traceback (most recent call last):
  File \”\”, line 1, in
    say()
TypeError: _test() takes exactly 1 argument (0 given)
>>>

由上面这段代码中的最后两个例子可知:当装饰器有参数时,即使你启用装饰器的默认参数,不另外传递新值进去,也必须有一对括号,否则编译器会直接将func传递给test(),而不是传递给_test()

b.被装饰对象有参数:

复制代码 代码如下:
>>> def test(printResult=False):
    def _test(func):
        def __test(*args,**kw):
            print \’Call the function %s().\’%func.func_name
            if printResult:
                print func(*args,**kw)
            else:
                return func(*args,**kw)
        return __test
    return _test

>>> @test()
def left(Str,Len):
    #The parameters of __test can be \'(Str,Len)\’ in this case.
    return Str[:Len]

>>> left(\’hello world\’,5)
Call the function left().
\’hello\’
>>> @test(True)
def left(Str,Len):
    #The parameters of __test can be \'(Str,Len)\’ in this case.
    return Str[:Len]

>>> left(\’hello world\’,5)
Call the function left().
hello
>>>

 
2.装饰类:被装饰的对象是一个类

[1]装饰器无参数:

a.被装饰对象无参数:

复制代码 代码如下:
>>> def test(cls):
    def _test():
        clsName=re.findall(\'(\\w+)\’,repr(cls))[-1]
        print \’Call %s.__init().\’%clsName
        return cls()
    return _test

>>> @test
class sy(object):
    value=32

   
>>> s=sy()
Call sy.__init().
>>> s
<__main__.sy object at 0x0000000002C3E390>
>>> s.value
32
>>>

b.被装饰对象有参数:
复制代码 代码如下:
>>> def test(cls):
    def _test(*args,**kw):
        clsName=re.findall(\'(\\w+)\’,repr(cls))[-1]
        print \’Call %s.__init().\’%clsName
        return cls(*args,**kw)
    return _test

>>> @test
class sy(object):
    def __init__(self,value):
                #The parameters of _test can be \'(value)\’ in this case.
        self.value=value

       
>>> s=sy(\’hello world\’)
Call sy.__init().
>>> s
<__main__.sy object at 0x0000000003AF7748>
>>> s.value
\’hello world\’
>>>

 [2]装饰器有参数:

a.被装饰对象无参数:

复制代码 代码如下:
>>> def test(printValue=True):
    def _test(cls):
        def __test():
            clsName=re.findall(\'(\\w+)\’,repr(cls))[-1]
            print \’Call %s.__init().\’%clsName
            obj=cls()
            if printValue:
                print \’value = %r\’%obj.value
            return obj
        return __test
    return _test

>>> @test()
class sy(object):
    def __init__(self):
        self.value=32

       
>>> s=sy()
Call sy.__init().
value = 32
>>> @test(False)
class sy(object):
    def __init__(self):
        self.value=32

       
>>> s=sy()
Call sy.__init().
>>>

 b.被装饰对象有参数:
 

复制代码 代码如下:
 >>> def test(printValue=True):
    def _test(cls):
        def __test(*args,**kw):
            clsName=re.findall(\'(\\w+)\’,repr(cls))[-1]
            print \’Call %s.__init().\’%clsName
            obj=cls(*args,**kw)
            if printValue:
                print \’value = %r\’%obj.value
            return obj
        return __test
    return _test

>>> @test()
class sy(object):
    def __init__(self,value):
        self.value=value

       
>>> s=sy(\’hello world\’)
Call sy.__init().
value = \’hello world\’
>>> @test(False)
class sy(object):
    def __init__(self,value):
        self.value=value

       
>>> s=sy(\’hello world\’)
Call sy.__init().
>>>
 

 二、类式装饰器:装饰器本身是一个类,借用__init__()和__call__()来实现职能

1.装饰函数:被装饰对象是一个函数

[1]装饰器无参数:

a.被装饰对象无参数:

复制代码 代码如下:
>>> class test(object):
    def __init__(self,func):
        self._func=func
    def __call__(self):
        return self._func()

   
>>> @test
def say():
    return \’hello world\’

>>> say()
\’hello world\’
>>>

b.被装饰对象有参数:

复制代码 代码如下:
>>> class test(object):
    def __init__(self,func):
        self._func=func
    def __call__(self,*args,**kw):
        return self._func(*args,**kw)

   
>>> @test
def left(Str,Len):
    #The parameters of __call__ can be \'(self,Str,Len)\’ in this case.
    return Str[:Len]

>>> left(\’hello world\’,5)
\’hello\’
>>>

 [2]装饰器有参数

a.被装饰对象无参数:

复制代码 代码如下:
>>> class test(object):
    def __init__(self,beforeinfo=\’Call function\’):
        self.beforeInfo=beforeinfo
    def __call__(self,func):
        def _call():
            print self.beforeInfo
            return func()
        return _call

   
>>> @test()
def say():
    return \’hello world\’

>>> say()
Call function
\’hello world\’
>>>

或者:

复制代码 代码如下:
 >>> class test(object):
    def __init__(self,beforeinfo=\’Call function\’):
        self.beforeInfo=beforeinfo
    def __call__(self,func):
        self._func=func
        return self._call
    def _call(self):
        print self.beforeInfo
        return self._func()

   
>>> @test()
def say():
    return \’hello world\’

>>> say()
Call function
\’hello world\’
>>>

 b.被装饰对象有参数:
 

复制代码 代码如下:
 >>> class test(object):
    def __init__(self,beforeinfo=\’Call function\’):
        self.beforeInfo=beforeinfo
    def __call__(self,func):
        def _call(*args,**kw):
            print self.beforeInfo
            return func(*args,**kw)
        return _call

   
>>> @test()
def left(Str,Len):
    #The parameters of _call can be \'(Str,Len)\’ in this case.
    return Str[:Len]

>>> left(\’hello world\’,5)
Call function
\’hello\’
>>>
 

 或者:
 

复制代码 代码如下:
 >>> class test(object):
    def __init__(self,beforeinfo=\’Call function\’):
        self.beforeInfo=beforeinfo
    def __call__(self,func):
        self._func=func
        return self._call
    def _call(self,*args,**kw):
        print self.beforeInfo
        return self._func(*args,**kw)

   
>>> @test()
def left(Str,Len):
    #The parameters of _call can be \'(self,Str,Len)\’ in this case.
    return Str[:Len]

>>> left(\’hello world\’,5)
Call function
\’hello\’
>>>
 

  2.装饰类:被装饰对象是一个类

[1]装饰器无参数:

a.被装饰对象无参数:

复制代码 代码如下:
>>> class test(object):
    def __init__(self,cls):
        self._cls=cls
    def __call__(self):
        return self._cls()

   
>>> @test
class sy(object):
    def __init__(self):
        self.value=32

   
>>> s=sy()
>>> s
<__main__.sy object at 0x0000000003AAFA20>
>>> s.value
32
>>>

 b.被装饰对象有参数:
 

复制代码 代码如下:
 >>> class test(object):
    def __init__(self,cls):
        self._cls=cls
    def __call__(self,*args,**kw):
        return self._cls(*args,**kw)

   
>>> @test
class sy(object):
    def __init__(self,value):
        #The parameters of __call__ can be \'(self,value)\’ in this case.
        self.value=value

       
>>> s=sy(\’hello world\’)
>>> s
<__main__.sy object at 0x0000000003AAFA20>
>>> s.value
\’hello world\’
>>>
 

 [2]装饰器有参数:

a.被装饰对象无参数:

复制代码 代码如下:
>>> class test(object):
    def __init__(self,printValue=False):
        self._printValue=printValue
    def __call__(self,cls):
        def _call():
            obj=cls()
            if self._printValue:
                print \’value = %r\’%obj.value
            return obj
        return _call

   
>>> @test(True)
class sy(object):
    def __init__(self):
        self.value=32

       
>>> s=sy()
value = 32
>>> s
<__main__.sy object at 0x0000000003AB50B8>
>>> s.value
32
>>>

 b.被装饰对象有参数:
 

复制代码 代码如下:
 >>> class test(object):
    def __init__(self,printValue=False):
        self._printValue=printValue
    def __call__(self,cls):
        def _call(*args,**kw):
            obj=cls(*args,**kw)
            if self._printValue:
                print \’value = %r\’%obj.value
            return obj
        return _call

   
>>> @test(True)
class sy(object):
    def __init__(self,value):
        #The parameters of _call can be \'(value)\’ in this case.
        self.value=value

       
>>> s=sy(\’hello world\’)
value = \’hello world\’
>>> s
<__main__.sy object at 0x0000000003AB5588>
>>> s.value
\’hello world\’
>>>
 

 总结:【1】@decorator后面不带括号时(也即装饰器无参数时),效果就相当于先定义func或cls,而后执行赋值操作func=decorator(func)或cls=decorator(cls);

【2】@decorator后面带括号时(也即装饰器有参数时),效果就相当于先定义func或cls,而后执行赋值操作 func=decorator(decoratorArgs)(func)或cls=decorator(decoratorArgs)(cls);

【3】如上将func或cls重新赋值后,此时的func或cls也不再是原来定义时的func或cls,而是一个可执行体,你只需要传入参数就可调用,func(args)=>返回值或者输出,cls(args)=>object of cls;

【4】最后通过赋值返回的执行体是多样的,可以是闭包,也可以是外部函数;当被装饰的是一个类时,还可以是类内部方法,函数;

【5】另外要想真正了解装饰器,一定要了解func.func_code.co_varnames,func.func_defaults,通过它们你可以以func的定义之外,还原func的参数列表;另外关键字参数是因为调用而出现的,而不是因为func的定义,func的定义中的用等号连接的只是有默认值的参数,它们并不一定会成为关键字参数,因为你仍然可以按照位置来传递它们。

相关内容

热门资讯

Mobi、epub格式电子书如... 在wps里全局设置里有一个文件关联,打开,勾选电子书文件选项就可以了。
定时清理删除C:\Progra... C:\Program Files (x86)下面很多scoped_dir开头的文件夹 写个批处理 定...
scoped_dir32_70... 一台虚拟机C盘总是莫名奇妙的空间用完,导致很多软件没法再运行。经过仔细检查发现是C:\Program...
500 行 Python 代码... 语法分析器描述了一个句子的语法结构,用来帮助其他的应用进行推理。自然语言引入了很多意外的歧义,以我们...
小程序支付时提示:appid和... [Q]小程序支付时提示:appid和mch_id不匹配 [A]小程序和微信支付没有进行关联,访问“小...
pycparser 是一个用... `pycparser` 是一个用 Python 编写的 C 语言解析器。它可以用来解析 C 代码并构...
微信小程序使用slider实现... 众所周知哈,微信小程序里面的音频播放是没有进度条的,但最近有个项目呢,客户要求音频要有进度条控制,所...
65536是2的几次方 计算2... 65536是2的16次方:65536=2⁶ 65536是256的2次方:65536=256 6553...
Apache Doris 2.... 亲爱的社区小伙伴们,我们很高兴地向大家宣布,Apache Doris 2.0.0 版本已于...
项目管理和工程管理的区别 项目管理 项目管理,顾名思义就是专注于开发和完成项目的管理,以实现目标并满足成功标准和项目要求。 工...