国产探花免费观看_亚洲丰满少妇自慰呻吟_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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 津市市| 渝中区| 玉林市| 天镇县| 含山县| 固原市| 黔西县| 德令哈市| 神池县| 牟定县| 天长市| 交口县| 泗阳县| 兰溪市| 巴青县| 松滋市| 泸水县| 乐陵市| 齐河县| 株洲县| 郸城县| 云霄县| 乐亭县| 沾益县| 南京市| 德江县| 万载县| 新密市| 拉萨市| 同仁县| 靖远县| 江源县| 金坛市| 尚志市| 吉安市| 通渭县| 安阳市| 离岛区| 巢湖市| 原阳县| 襄城县|