這篇文章主要介紹python如何實(shí)現(xiàn)裝飾器,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

1. 普通裝飾器
import logging
1. foo = use_loggine(foo)
def use_loggine(func):
def wrapper():
logging.warn("%s is running " % func.__name__)
return func()
return wrapper
@use_loggine
def foo():
print "aaa"
foo()
print foo.__name__2. func 需要參數(shù):
foo = use_loggine(foo) 第一個(gè)參數(shù)就是func這個(gè)函數(shù)對(duì)象,不包含參數(shù)foo(params)
def use_loggine(func):
def wrapper(name):
logging.warn("%s is running " % func.__name__)
return func(name)
return wrapper
@use_loggine
def foo(name):
print "name is %s" % name3. 裝飾器帶參數(shù)
foo = use_logging('warn')(foo) 還是把被裝飾的函數(shù)當(dāng)做參數(shù)賦給裝飾器
def use_logging(level):
def decorator(func):
def wrapper(*args, **kwargs):
if level == 'warn':
logging.warn("%s is running" % func.__name__)
elif level == 'info':
logging.warn("%s is running" % func.__name__)
return func(*args)
return wrapper
return decorator
@use_logging('warn')
def foo(name):
print "i am %s" % name
foo = use_logging('warn')(foo)
print foo.__name__
foo('foo')4. 類裝飾器,還是把被裝飾的函數(shù)當(dāng)做參數(shù)賦給裝飾器
foo = Foo(params)(func)
class Foo(object):
def __init__(self, name):
self.name = name
def __call__(self, func):
def aa():
print "class decorator running"
print "name is :%s" % self.name
func()
print 'class decorator ending'
return aa
@Foo("hello")
def bar():
print "world"
bar()
@deco
def foo()
pass
foo = deco(foo)
@deco(xx)
def foo():
pass
foo = deco(xx)(foo)5. 類方法裝飾器
類方法裝飾器和其他裝飾器沒有什么區(qū)別,只不過在裝飾器內(nèi)部返回的函數(shù)中,第一個(gè)參數(shù)是固定的,是調(diào)用方法的對(duì)象本身,如
果是實(shí)例對(duì)象,就是self,是類方法的話,就是cls,靜態(tài)方法的話,沒有第一個(gè)參數(shù)。
from functools import wraps
def method_decor(func):
@wraps(func)
def wrapper(*args, **kwargs):
try:
u = func(*args, **kwargs)
return u
except Exception as e:
args[0].bb() # args[0]就是self或者cls。可以在args前面定義self,更加方便u = func(self,*args, **kwargs)
return 'an Exception raised.'
return wrapper
class Foo(object):
@method_decor
def aa(self):
# print("This is wraped method")
raise Exception('aa')
def bb(self):
print("This is called method")
a = Foo()
a.aa()以上是“python如何實(shí)現(xiàn)裝飾器”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊頻道!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
當(dāng)前題目:python如何實(shí)現(xiàn)裝飾器-創(chuàng)新互聯(lián)
URL分享:http://www.chinadenli.net/article0/idoio.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、移動(dòng)網(wǎng)站建設(shè)、面包屑導(dǎo)航、ChatGPT、定制網(wǎng)站、動(dòng)態(tài)網(wǎng)站
聲明:本網(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í)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容