您好,登錄后才能下訂單哦!
裝飾器:原函數基礎 ------> 動態添加功能
一、不帶參數的decorator
#decorator封裝
def log(func):
def wrapper(*args, **kw):
print 'call %s():' %func.__name__
return func(*args, **kw)
return wrapper
#此函數一定要放在 def log(func): 之后
@log
def now():
print "haha"
now()
log(now())
print "now_name:%s"%now.__name__
簡單的圖解:(待以后深入理解后再詳細補)
二、帶參數的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內置的functools.wraps
import functools #導入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
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
補充:
多個裝飾器的執行順序是無序的,但執行過程是有序的。
裝飾器執行過程是:遇到外接函數就跳轉到另一個裝飾器中執行,若再另一個裝飾器中遇到同樣的外接函數,則繼續執行
證明1:多個裝飾器的執行順序是無序的
#!/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)
執行結果:先從第一個裝飾器標志@extend開始
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
另一個例子:
#!/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()
運行結果:從第二個裝飾器標志@dec2開始
aaaa 1111 2222 bbbb test test cccc 3333
通過比較,多個解釋器的執行順序是無序的。若大家覺得證明不夠嚴謹,可自己多添幾個試試。
證明2:裝飾器的執行過程是有序的
圖1:
圖2:
沒看懂?沒關系,進行步驟解析:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2018/8/20
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
補充:一個講的很好的BLOG: https://blog.csdn.net/xiangxianghehe/article/details/77170585
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。