您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python中的各种装饰器详解

5b51 2022/1/14 8:19:23 python 字数 12205 阅读 395 来源 www.jb51.cc/python

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

概述

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

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

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

[1]装饰器无参数:

a.被装饰对象无参数:

>>> @test
def say():return 'hello world'

>>> say()
Call the function say().
'hello world'
>>>

b.被装饰对象有参数:

>>> @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.被装饰对象无参数:

>>> @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 "<pyshell#224>",line 1,in <module>
    say()
TypeError: _test() takes exactly 1 argument (0 given)
>>>

b.被装饰对象有参数:

>>> @test()
def left(Str,Len):
    #The parameters of __test can be '(Str,5)
Call the function left().
'hello'
>>> @test(True)
def left(Str,5)
Call the function left().
hello
>>>

[1]装饰器无参数:

a.被装饰对象无参数:

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

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

>>> @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'
>>>

a.被装饰对象无参数:

>>> @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.被装饰对象有参数:
 

>>> @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.被装饰对象无参数:

   
>>> @test
def say():
    return 'hello world'

>>> say()
'hello world'
>>>

b.被装饰对象有参数:

   
>>> @test
def left(Str,Len):
    #The parameters of __call__ can be '(self,Str,5)
'hello'
>>>

 [2]装饰器有参数

a.被装饰对象无参数:

   
>>> @test()
def say():
    return 'hello world'

>>> say()
Call function
'hello world'
>>>

或者:

   
>>> @test()
def say():
    return 'hello world'

>>> say()
Call function
'hello world'
>>>

 b.被装饰对象有参数:
 

   
>>> @test()
def left(Str,Len):
    #The parameters of _call can be '(Str,5)
Call function
'hello'
>>>
 

 或者:
 

   
>>> @test()
def left(Str,Len):
    #The parameters of _call can be '(self,5)
Call function
'hello'
>>>
 

[1]装饰器无参数:

a.被装饰对象无参数:

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

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

 b.被装饰对象有参数:
 

   
>>> @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.被装饰对象无参数:

   
>>> @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.被装饰对象有参数:
 

   
>>> @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的定义中的用等号连接的只是有认值的参数,它们并不一定会成为关键字参数,因为你仍然可以按照位置来传递它们。

总结

以上是编程之家为你收集整理的Python中的各种装饰器详解全部内容,希望文章能够帮你解决Python中的各种装饰器详解所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶