python裝飾器的寫法有哪些?這個(gè)問(wèn)題可能是我們?nèi)粘W(xué)習(xí)或工作經(jīng)常見到的。希望通過(guò)這個(gè)問(wèn)題能讓你收獲頗深。下面是小編給大家?guī)?lái)的參考內(nèi)容,讓我們一起來(lái)看看吧!

裝飾器的示例代碼
# 定義裝飾器
def decorator(func):
def inner():
# 在內(nèi)部函數(shù)里面對(duì)已有函數(shù)進(jìn)行裝飾
print('已添加登錄認(rèn)證')
func()
return inner
def comment():
print('發(fā)表評(píng)論')
# 調(diào)用裝飾器對(duì)已有函數(shù)進(jìn)行裝飾,左邊的comment=inner
comment = decorator(comment)
# 調(diào)用方式不變
comment()裝飾器的語(yǔ)法糖寫法
如果有多個(gè)函數(shù)都需要添加登錄驗(yàn)證的功能,每次都需要編寫func = decorator(func)這樣代碼對(duì)已有函數(shù)進(jìn)行裝飾,這種做法還是比較麻煩。
Python給提供了一個(gè)裝飾函數(shù)更加簡(jiǎn)單的寫法,那就是語(yǔ)法糖,語(yǔ)法糖的書寫格式是: @裝飾器名字,通過(guò)語(yǔ)法糖的方式也可以完成對(duì)已有函數(shù)的裝飾。
# 定義裝飾器
def decorator(func):
def inner():
# 在內(nèi)部函數(shù)里面對(duì)已有函數(shù)進(jìn)行裝飾
print('已添加登錄認(rèn)證')
func()
return inner
@decorator # comment = decorator(comment) 裝飾器語(yǔ)法糖對(duì)該代碼進(jìn)行了封裝 左邊comment=inner
def comment():
print('發(fā)表評(píng)論')
# 調(diào)用方式不變
comment()裝飾器的執(zhí)行時(shí)機(jī)
當(dāng)前模塊加載完成以后,裝飾器會(huì)立即執(zhí)行,對(duì)已有函數(shù)進(jìn)行裝飾。
# 定義裝飾器
def decorator(func):
print('裝飾器執(zhí)行了')
def inner():
# 在內(nèi)部函數(shù)里面對(duì)已有函數(shù)進(jìn)行裝飾
print('已添加登錄認(rèn)證')
func()
return inner
@decorator # comment = decorator(comment) 裝飾器語(yǔ)法糖對(duì)該代碼進(jìn)行了封裝 左邊comment=inner
def comment():
print('發(fā)表評(píng)論')運(yùn)行結(jié)果
裝飾器執(zhí)行了
1
裝飾器實(shí)現(xiàn)已有函數(shù)執(zhí)行時(shí)間的統(tǒng)計(jì)
import time
def decorator(func):
def inner():
# 獲取時(shí)間距離1970-1-1 0:0:1的時(shí)間差
begin = time.time()
func()
end = time.time()
result = end - begin
print(f'函數(shù)執(zhí)行完成耗時(shí):{result}')
return inner
@decorator
def work():
for i in range(10000):
print(i)
work()裝飾帶有參數(shù)的函數(shù)
def decorator(func):
def inner(num1, num2):
print('正在努力執(zhí)行加法計(jì)算')
func(num1, num2)
return inner
@decorator
def add_num(num1, num2):
result = num1 + num2
print(f'結(jié)果為:{result}')
add_num(1, 2)裝飾帶有參數(shù)、返回值的函數(shù)
def decorator(func):
def inner(num1, num2):
print('正在努力執(zhí)行加法計(jì)算')
num = func(num1, num2)
return num
return inner
@decorator
def add_num(num1, num2):
result = num1 + num2
return result
result = add_num(1, 2)
print(f'結(jié)果為:{result}')類裝飾器的使用
class MyDecorator(object):
def __init__(self, func):
self.__func = func
# 實(shí)現(xiàn)__call__方法,表示對(duì)象是一個(gè)可調(diào)用對(duì)象,可以像調(diào)用函數(shù)一樣進(jìn)行調(diào)用
def __call__(self, *args, **kwargs):
# 對(duì)已有函數(shù)進(jìn)行封裝
print('馬上就有下班啦')
self.__func()
@MyDecorator # @MyDecorator => show = MyDecorator(show)
def show():
print('快要下雪啦')
# 執(zhí)行show,就相當(dāng)于執(zhí)行MyDecorator類創(chuàng)建的實(shí)例對(duì)象,show() => 對(duì)象()
show()擴(kuò)展:
函數(shù)之所以能夠調(diào)用,是因?yàn)楹瘮?shù)內(nèi)部實(shí)現(xiàn)了 __call__ 方法
感謝各位的閱讀!看完上述內(nèi)容,你們對(duì)python裝飾器的寫法有哪些大概了解了嗎?希望文章內(nèi)容對(duì)大家有所幫助。如果想了解更多相關(guān)文章內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)頁(yè)題目:python裝飾器的寫法有哪些-創(chuàng)新互聯(lián)
網(wǎng)頁(yè)URL:http://www.chinadenli.net/article44/ccchee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、動(dòng)態(tài)網(wǎng)站、品牌網(wǎng)站設(shè)計(jì)、軟件開發(fā)、手機(jī)網(wǎng)站建設(shè)、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容