動(dòng)態(tài)添加功能一、不帶參數(shù)的decorator#decorator封裝def log(func): def wrapper(*args, **kw): &nbs..."/>
您好,登錄后才能下訂單哦!
裝飾器:原函數(shù)基礎(chǔ) ------> 動(dòng)態(tài)添加功能
一、不帶參數(shù)的decorator
#decorator封裝
def log(func):
def wrapper(*args, **kw):
print 'call %s():' %func.__name__
return func(*args, **kw)
return wrapper
#此函數(shù)一定要放在 def log(func): 之后
@log
def now():
print "haha"
now()
log(now())
print "now_name:%s"%now.__name__
簡(jiǎn)單的圖解:(待以后深入理解后再詳細(xì)補(bǔ))
二、帶參數(shù)的docurator
#!/usr/bin/python
#docurator封裝
def log(text):
def decorator(func):
def wrapper(*args, **kw):
print('%s %s():' % (text, func.__name__))
return func(*args, **kw)
return wrapper
return decorator
@log('execute')
def now():
print "haha"
print "now_name:%s"%now.__name__
now()
print log('execute')
print log(now)
print log('execute')(now)
三、完整的decorator的寫法:Python內(nèi)置的functools.wraps
import functools #導(dǎo)入functools模塊
def log(text):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kw):
print('%s %s():' % (text, func.__name__))
return func(*args, **kw)
return wrapper
return decorator
@log('execute')
def now():
print "haha"
print "now_name:%s"%now.__name__
now()
print log('execute')
print log(now)
print log('execute')(now)
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
日期:2018/8/18
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
補(bǔ)充:
多個(gè)裝飾器的執(zhí)行順序是無(wú)序的,但執(zhí)行過(guò)程是有序的。
裝飾器執(zhí)行過(guò)程是:遇到外接函數(shù)就跳轉(zhuǎn)到另一個(gè)裝飾器中執(zhí)行,若再另一個(gè)裝飾器中遇到同樣的外接函數(shù),則繼續(xù)執(zhí)行
證明1:多個(gè)裝飾器的執(zhí)行順序是無(wú)序的
#!/usr/bin/python # -*- coding: utf-8 -*- import time def extend(func): def wrapper(*args, **kwargs): print "Begin AAAAAA" func(*args, **kwargs) print("A:%s"%(func.__name__)) print(reduce(lambda x,y:x+y, args), kwargs) print "End AAAAA" return wrapper def extend1(func): def wrapper(*args, **kwargs): print "Begin BBBBB" func(*args, **kwargs) print("B:%s"%(func.__name__)) print(map(lambda x:x*2, args), kwargs) print "End BBBBB" return wrapper @extend @extend1 def get_time1(*args,**kwargs): begin_time = time.time() for n in range(3,4): time.sleep(1) end_time = time.time() print("run %d "% (end_time - begin_time)) #print(reduce(lambda x,y:x+y, args), kwargs) if __name__ == "__main__": get_time1(1,2,3,4,5,6,ma=1,du=2)
執(zhí)行結(jié)果:先從第一個(gè)裝飾器標(biāo)志@extend開(kāi)始
Begin AAAAAA Begin BBBBB run 1 B:get_time1 ([2, 4, 6, 8, 10, 12], {'du': 2, 'ma': 1}) End BBBBB A:wrapper (21, {'du': 2, 'ma': 1}) End AAAAA
另一個(gè)例子:
#!/usr/bin/python # -*- coding: utf-8 -*- def dec1(func): print("1111") def wrapper(*args, **kwargs): print("2222") func() print("3333") return wrapper def dec2(func): print("aaaa") def wrapper(*args, **kwargs): print("bbbb") func() print("cccc") return wrapper @dec1 @dec2 def test(): print("test test") test()
運(yùn)行結(jié)果:從第二個(gè)裝飾器標(biāo)志@dec2開(kāi)始
aaaa 1111 2222 bbbb test test cccc 3333
通過(guò)比較,多個(gè)解釋器的執(zhí)行順序是無(wú)序的。若大家覺(jué)得證明不夠嚴(yán)謹(jǐn),可自己多添幾個(gè)試試。
證明2:裝飾器的執(zhí)行過(guò)程是有序的
圖1:
圖2:
沒(méi)看懂?沒(méi)關(guān)系,進(jìn)行步驟解析:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2018/8/20
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
補(bǔ)充:一個(gè)講的很好的BLOG: https://blog.csdn.net/xiangxianghehe/article/details/77170585
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。