【转】python装饰器
什么是裝飾器?
?python裝飾器(fuctional decorators)就是用于拓展原來函數(shù)功能的一種函數(shù),目的是在不改變原函數(shù)名(或類名)的情況下,給函數(shù)增加新的功能。?
這個函數(shù)的特殊之處在于它的返回值也是一個函數(shù),這個函數(shù)是內(nèi)嵌“原“”函數(shù)的函數(shù)。
一般而言,我們要想拓展原來函數(shù)代碼,最直接的辦法就是侵入代碼里面修改,例如:
?
這是我們最原始的的一個函數(shù),然后我們試圖記錄下這個函數(shù)執(zhí)行的總時間,那最簡單的做法就是改動原來的代碼:
import time def f():start_time = time.time()print("hello")time.sleep(1)print("world")end_time = time.time()execution_time = (end_time - start_time)*1000print("time is %d ms" %execution_time)?
但是實際工作中,有些時候核心代碼并不可以直接去改,所以在不改動原代碼的情況下,我們可以再定義一個函數(shù)。(但是生效需要再次執(zhí)行函數(shù))
import timedef deco(func):start_time = time.time()f()end_time = time.time()execution_time = (end_time - start_time)*1000print("time is %d ms" %execution_time)def f():print("hello")time.sleep(1)print("world")if __name__ == '__main__':deco(f)print("f.__name__ is",f.__name__)print()?
這里我們定義了一個函數(shù)deco,它的參數(shù)是一個函數(shù),然后給這個函數(shù)嵌入了計時功能。但是想要拓展這一千萬個函數(shù)功能,
就是要執(zhí)行一千萬次deco()函數(shù),所以這樣并不理想!接下來,我們可以試著用裝飾器來實現(xiàn),先看看裝飾器最原始的面貌。?
import timedef deco(f):def wrapper():start_time = time.time()f()end_time = time.time()execution_time = (end_time - start_time)*1000print("time is %d ms" %execution_time )return wrapper@deco def f():print("hello")time.sleep(1)print("world")if __name__ == '__main__':f()這里的deco函數(shù)就是最原始的裝飾器,它的參數(shù)是一個函數(shù),然后返回值也是一個函數(shù)。
其中作為參數(shù)的這個函數(shù)f()就在返回函數(shù)wrapper()的內(nèi)部執(zhí)行。然后在函數(shù)f()前面加上@deco,
f()函數(shù)就相當(dāng)于被注入了計時功能,現(xiàn)在只要調(diào)用f(),它就已經(jīng)變身為“新的功能更多”的函數(shù)了,
(不需要重復(fù)執(zhí)行原函數(shù))。?
?
擴展1:帶有固定參數(shù)的裝飾器
import timedef deco(f):def wrapper(a,b):start_time = time.time()f(a,b)end_time = time.time()execution_time = (end_time - start_time)*1000print("time is %d ms" % execution_time)return wrapper@deco def f(a,b):print("be on")time.sleep(1)print("result is %d" %(a+b))if __name__ == '__main__':f(3,4)擴展2:無固定參數(shù)的裝飾器
import timedef deco(f):def wrapper(*args, **kwargs):start_time = time.time()f(*args, **kwargs)end_time = time.time()execution_time_ = (end_time - start_time)*1000print("time is %d ms" %execution_time)return wrapper@deco def f(a,b):print("be on")time.sleep(1)print("result is %d" %(a+b))@deco def f2(a,b,c):print("be on")time.sleep(1)print("result is %d" %(a+b+c))if __name__ == '__main__':f2(3,4,5)f(3,4)擴展3:使用多個裝飾器,裝飾一個函數(shù)
import timedef deco01(f):def wrapper(*args, **kwargs):print("this is deco01")start_time = time.time()f(*args, **kwargs)end_time = time.time()execution_time = (end_time - start_time)*1000print("time is %d ms" % execution_time)print("deco01 end here")return wrapperdef deco02(f):def wrapper(*args, **kwargs):print("this is deco02")f(*args, **kwargs)print("deco02 end here")return wrapper@deco01 @deco02 def f(a,b):print("be on")time.sleep(1)print("result is %d" %(a+b))if __name__ == '__main__':f(3,4) ''' this is deco01 this is deco02 hello,here is a func for add : result is 7 deco02 end here time is 1003 ms deco01 end here '''?
裝飾器調(diào)用順序:
飾器是可以疊加使用的,那么使用裝飾器以后代碼是啥順序呢?
對于Python中的”@”語法糖,裝飾器的調(diào)用順序與使用 @ 語法糖聲明的順序相反。
在這個例子中,”f(3, 4) = deco01(deco02(f(3, 4)))”。
?
Python內(nèi)置裝飾器
在Python中有三個內(nèi)置的裝飾器,都是跟class相關(guān)的:staticmethod、classmethod 和property。
- staticmethod 是類靜態(tài)方法,其跟成員方法的區(qū)別是沒有 self 參數(shù),并且可以在類不進行實例化的情況下調(diào)用
- classmethod 與成員方法的區(qū)別在于所接收的第一個參數(shù)不是 self (類實例的指針),而是cls(當(dāng)前類的具體類型)
- property 是屬性的意思,表示可以通過通過類實例直接訪問的信息
對于staticmethod和classmethod這里就不介紹了,通過一個例子看看property。
注意,對于Python新式類(new-style class),如果將上面的 “@var.setter” 裝飾器所裝飾的成員函數(shù)去掉,則Foo.var 屬性為只讀屬性,使用 “foo.var = ‘var 2′” 進行賦值時會拋出異常。但是,對于Python classic class,所聲明的屬性不是 read-only的,所以即使去掉”@var.setter”裝飾器也不會報錯。
轉(zhuǎn)載于:https://www.cnblogs.com/HYanqing/p/11207269.html
總結(jié)
以上是生活随笔為你收集整理的【转】python装饰器的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Mysql 中根据条件排序获取排名
- 下一篇: 利用python 实现微信公众号群发图片