国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python中的多重裝飾器

2020-02-23 00:38:31
字體:
來源:轉載
供稿:網友

多重裝飾器,即多個裝飾器修飾同一個對象【實際上并非完全如此,且看下文詳解】

1.裝飾器無參數:
代碼如下:
>>> def first(func):
    print '%s() was post to first()'%func.func_name
    def _first(*args,**kw):
        print 'Call the function %s() in _first().'%func.func_name
        return func(*args,**kw)
    return _first


>>> def second(func):
    print '%s() was post to second()'%func.func_name
    def _second(*args,**kw):
        print 'Call the function %s() in _second().'%func.func_name
        return func(*args,**kw)
    return _second


>>> @first
@second
def test():return 'hello world'

test() was post to second()
_second() was post to first()
>>> test()
Call the function _second() in _first().
Call the function test() in _second().
'hello world'
>>>

實際上它是相當于下面的代碼:
代碼如下:
>>> def test():
    return 'hello world'

>>> test=second(test)
test() was post to second()
>>> test
<function _second at 0x000000000316D3C8>
>>> test=first(test)
_second() was post to first()
>>> test
<function _first at 0x000000000316D358>
>>> test()
Call the function _second() in _first().
Call the function test() in _second().
'hello world'
>>>

2.裝飾器有參數:
代碼如下:
>>> def first(printResult=False):
    def _first(func):
        print '%s() was post to _first()'%func.func_name
        def __first(*args,**kw):
            print 'Call the function %s() in __first().'%/
                  func.func_name
            if printResult:
                print func(*args,**kw),'#print in __first().'
            else:
                return func(*args,**kw)
        return __first
    return _first

>>> def second(printResult=False):
    def _second(func):
        print '%s() was post to _second()'%func.func_name

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 肇东市| 康定县| 资中县| 龙泉市| 达日县| 渭南市| 安塞县| 特克斯县| 内乡县| 平邑县| 民乐县| 沈阳市| 柯坪县| 安泽县| 沂南县| 金寨县| 鸡西市| 汽车| 龙井市| 东台市| 海兴县| 合川市| 平乡县| 兴仁县| 云和县| 红河县| 包头市| 新建县| 增城市| 个旧市| 庆阳市| 香河县| 武川县| 米易县| 柳州市| 张家口市| 萝北县| 元阳县| 安塞县| 百色市| 夏邑县|