午夜国产狂喷潮在线观看|国产AⅤ精品一区二区久久|中文字幕AV中文字幕|国产看片高清在线

    Python中的各種裝飾器詳解
    來(lái)源:易賢網(wǎng) 閱讀:1605 次 日期:2015-04-14 15:29:30
    溫馨提示:易賢網(wǎng)小編為您整理了“Python中的各種裝飾器詳解”,方便廣大網(wǎng)友查閱!

    Python裝飾器,分兩部分,一是裝飾器本身的定義,一是被裝飾器對(duì)象的定義。

    一、函數(shù)式裝飾器:裝飾器本身是一個(gè)函數(shù)。

    1.裝飾函數(shù):被裝飾對(duì)象是一個(gè)函數(shù)

    [1]裝飾器無(wú)參數(shù):

    a.被裝飾對(duì)象無(wú)參數(shù):

    代碼如下:

    >>> 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.被裝飾對(duì)象有參數(shù):

    代碼如下:

    >>> 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]裝飾器有參數(shù):

    a.被裝飾對(duì)象無(wú)參數(shù):

    代碼如下:

    >>> 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 "<pyshell#224>", line 1, in <module>

    say()

    TypeError: _test() takes exactly 1 argument (0 given)

    >>>

    由上面這段代碼中的最后兩個(gè)例子可知:當(dāng)裝飾器有參數(shù)時(shí),即使你啟用裝飾器的默認(rèn)參數(shù),不另外傳遞新值進(jìn)去,也必須有一對(duì)括號(hào),否則編譯器會(huì)直接將func傳遞給test(),而不是傳遞給_test()

    b.被裝飾對(duì)象有參數(shù):

    代碼如下:

    >>> 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.裝飾類(lèi):被裝飾的對(duì)象是一個(gè)類(lèi)

    [1]裝飾器無(wú)參數(shù):

    a.被裝飾對(duì)象無(wú)參數(shù):

    代碼如下:

    >>> 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.被裝飾對(duì)象有參數(shù):

    代碼如下:

    >>> 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]裝飾器有參數(shù):

    a.被裝飾對(duì)象無(wú)參數(shù):

    代碼如下:

    >>> 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.被裝飾對(duì)象有參數(shù):

    代碼如下:

    >>> 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().

    >>>

    二、類(lèi)式裝飾器:裝飾器本身是一個(gè)類(lèi),借用__init__()和__call__()來(lái)實(shí)現(xiàn)職能

    1.裝飾函數(shù):被裝飾對(duì)象是一個(gè)函數(shù)

    [1]裝飾器無(wú)參數(shù):

    a.被裝飾對(duì)象無(wú)參數(shù):

    代碼如下:

    >>> 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.被裝飾對(duì)象有參數(shù):

    代碼如下:

    >>> 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]裝飾器有參數(shù)

    a.被裝飾對(duì)象無(wú)參數(shù):

    代碼如下:

    >>> 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.被裝飾對(duì)象有參數(shù):

    代碼如下:

    >>> 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.裝飾類(lèi):被裝飾對(duì)象是一個(gè)類(lèi)

    [1]裝飾器無(wú)參數(shù):

    a.被裝飾對(duì)象無(wú)參數(shù):

    代碼如下:

    >>> 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.被裝飾對(duì)象有參數(shù):

    代碼如下:

    >>> 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]裝飾器有參數(shù):

    a.被裝飾對(duì)象無(wú)參數(shù):

    代碼如下:

    >>> 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.被裝飾對(duì)象有參數(shù):

    代碼如下:

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

    >>>

    總結(jié):【1】@decorator后面不帶括號(hào)時(shí)(也即裝飾器無(wú)參數(shù)時(shí)),效果就相當(dāng)于先定義func或cls,而后執(zhí)行賦值操作func=decorator(func)或cls=decorator(cls);

    【2】@decorator后面帶括號(hào)時(shí)(也即裝飾器有參數(shù)時(shí)),效果就相當(dāng)于先定義func或cls,而后執(zhí)行賦值操作 func=decorator(decoratorArgs)(func)或cls=decorator(decoratorArgs)(cls);

    【3】如上將func或cls重新賦值后,此時(shí)的func或cls也不再是原來(lái)定義時(shí)的func或cls,而是一個(gè)可執(zhí)行體,你只需要傳入?yún)?shù)就可調(diào)用,func(args)=>返回值或者輸出,cls(args)=>object of cls;

    【4】最后通過(guò)賦值返回的執(zhí)行體是多樣的,可以是閉包,也可以是外部函數(shù);當(dāng)被裝飾的是一個(gè)類(lèi)時(shí),還可以是類(lèi)內(nèi)部方法,函數(shù);

    【5】另外要想真正了解裝飾器,一定要了解func.func_code.co_varnames,func.func_defaults,通過(guò)它們你可以以func的定義之外,還原func的參數(shù)列表;另外關(guān)鍵字參數(shù)是因?yàn)檎{(diào)用而出現(xiàn)的,而不是因?yàn)閒unc的定義,func的定義中的用等號(hào)連接的只是有默認(rèn)值的參數(shù),它們并不一定會(huì)成為關(guān)鍵字參數(shù),因?yàn)槟闳匀豢梢园凑瘴恢脕?lái)傳遞它們。

    更多信息請(qǐng)查看IT技術(shù)專欄

    更多信息請(qǐng)查看腳本欄目
    易賢網(wǎng)手機(jī)網(wǎng)站地址:Python中的各種裝飾器詳解
    由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門(mén)公布的正式信息和咨詢?yōu)闇?zhǔn)!

    2025國(guó)考·省考課程試聽(tīng)報(bào)名

    • 報(bào)班類(lèi)型
    • 姓名
    • 手機(jī)號(hào)
    • 驗(yàn)證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
    工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
    云南網(wǎng)警備案專用圖標(biāo)
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
    云南網(wǎng)警報(bào)警專用圖標(biāo)