網(wǎng)站首頁 編程語言 正文
functools模塊是Python的標(biāo)準(zhǔn)庫的一部分,它是為高階函數(shù)而實(shí)現(xiàn)的。高階函數(shù)是作用于或返回另一個(gè)函數(shù)或多個(gè)函數(shù)的函數(shù)。一般來說,對這個(gè)模塊而言,任何可調(diào)用的對象都可以作為一個(gè)函數(shù)來處理。
functools 提供了 11個(gè)函數(shù):?
1. cached_property
將類的方法轉(zhuǎn)換為一個(gè)屬性,該屬性的值計(jì)算一次,然后在實(shí)例的生命周期中將其緩存作為普通屬性。與 property() 類似,但添加了緩存,對于在其他情況下實(shí)際不可變的高計(jì)算資源消耗的實(shí)例特征屬性來說該函數(shù)非常有用。
# cached_property 緩存屬性 class cached_property(object): """ Decorator that converts a method with a single self argument into a property cached on the instance. Optional ``name`` argument allows you to make cached properties of other methods. (e.g. url = cached_property(get_absolute_url, name='url') ) """ def __init__(self, func, name=None): # print(f'f: {id(func)}') self.func = func self.__doc__ = getattr(func, '__doc__') self.name = name or func.__name__ def __get__(self, instance, type=None): # print(f'self func: {id(self.func)}') # print(f'instance: {id(instance)}') if instance is None: return self res = instance.__dict__[self.name] = self.func(instance) return res class F00(): @cached_property def test(self): # cached_property 將會(huì)把每個(gè)實(shí)例的屬性存儲(chǔ)到實(shí)例的__dict__中, 實(shí)例獲取屬性時(shí), 將會(huì)優(yōu)先從__dict__中獲取,則不會(huì)再次調(diào)用方法內(nèi)部的過程 print(f'運(yùn)行test方法內(nèi)部過程') return 3 @property def t(self): print('運(yùn)行t方法內(nèi)部過程') return 44 f = F00() print(f.test) # 第一次將會(huì)調(diào)用test方法內(nèi)部過程 print(f.test) # 再次調(diào)用將直接從實(shí)例中的__dict__中直接獲取,不會(huì)再次調(diào)用方法內(nèi)部過程 print(f.t) # 調(diào)用方法內(nèi)部過程取值 print(f.t) # 調(diào)用方法內(nèi)部過程取值 # 結(jié)果輸出 # 運(yùn)行test方法內(nèi)部過程 # 3 # 3 # 運(yùn)行t方法內(nèi)部過程 # 44 # 運(yùn)行t方法內(nèi)部過程 # 44
2. cmp_to_key
在 list.sort 和 內(nèi)建函數(shù) sorted 中都有一個(gè) key 參數(shù)
x = ['hello','worl','ni'] x.sort(key=len) print(x) # ['ni', 'worl', 'hello']
3. lru_cache
允許我們將一個(gè)函數(shù)的返回值快速地緩存或取消緩存。
該裝飾器用于緩存函數(shù)的調(diào)用結(jié)果,對于需要多次調(diào)用的函數(shù),而且每次調(diào)用參數(shù)都相同,則可以用該裝飾器緩存調(diào)用結(jié)果,從而加快程序運(yùn)行。
該裝飾器會(huì)將不同的調(diào)用結(jié)果緩存在內(nèi)存中,因此需要注意內(nèi)存占用問題。
from functools import lru_cache @lru_cache(maxsize=30) # maxsize參數(shù)告訴lru_cache緩存最近多少個(gè)返回值 def fib(n): if n < 2: return n return fib(n-1) + fib(n-2) print([fib(n) for n in range(10)]) fib.cache_clear() # 清空緩存
4. partial
用于創(chuàng)建一個(gè)偏函數(shù),將默認(rèn)參數(shù)包裝一個(gè)可調(diào)用對象,返回結(jié)果也是可調(diào)用對象。
偏函數(shù)可以固定住原函數(shù)的部分參數(shù),從而在調(diào)用時(shí)更簡單。
from functools import partial int2 = partial(int, base=8) print(int2('123')) # 83
5. partialmethod
對于python 偏函數(shù)partial理解運(yùn)用起來比較簡單,就是對原函數(shù)某些參數(shù)設(shè)置默認(rèn)值,生成一個(gè)新函數(shù)。而如果對于類方法,因?yàn)榈谝粋€(gè)參數(shù)是 self,使用 partial 就會(huì)報(bào)錯(cuò)了。
class functools.partialmethod(func, /, *args, **keywords)
返回一個(gè)新的 partialmethod 描述器,其行為類似 partial 但它被設(shè)計(jì)用作方法定義而非直接用作可調(diào)用對象。
func 必須是一個(gè) descriptor 或可調(diào)用對象(同屬兩者的對象例如普通函數(shù)會(huì)被當(dāng)作描述器來處理)。
當(dāng) func 是一個(gè)描述器(例如普通 Python 函數(shù), classmethod(), staticmethod(), abstractmethod() 或其他 partialmethod 的實(shí)例)時(shí), 對 __get__
的調(diào)用會(huì)被委托給底層的描述器,并會(huì)返回一個(gè)適當(dāng)?shù)?部分對象 作為結(jié)果。
當(dāng) func 是一個(gè)非描述器類可調(diào)用對象時(shí),則會(huì)動(dòng)態(tài)創(chuàng)建一個(gè)適當(dāng)?shù)慕壎ǚ椒ā?當(dāng)用作方法時(shí)其行為類似普通 Python 函數(shù):將會(huì)插入 self 參數(shù)作為第一個(gè)位置參數(shù),其位置甚至?xí)幱谔峁┙o partialmethod 構(gòu)造器的 args 和 keywords 之前。
from functools import partialmethod class Cell: def __init__(self): self._alive = False @property def alive(self): return self._alive def set_state(self, state): self._alive = bool(state) set_alive = partialmethod(set_state, True) set_dead = partialmethod(set_state, False) print(type(partialmethod(set_state, False))) # <class 'functools.partialmethod'> c = Cell() c.alive # False c.set_alive() c.alive # True
6. reduce
函數(shù)的作用是將一個(gè)序列歸納為一個(gè)輸出reduce(function, sequence, startValue)?
from functools import reduce l = range(1,50) print(reduce(lambda x,y:x+y, l)) # 1225
7. singledispatch
單分發(fā)器,用于實(shí)現(xiàn)泛型函數(shù)。根據(jù)單一參數(shù)的類型來判斷調(diào)用哪個(gè)函數(shù)。
from functools import singledispatch @singledispatch def fun(text): print('String:' + text) @fun.register(int) def _(text): print(text) @fun.register(list) def _(text): for k, v in enumerate(text): print(k, v) @fun.register(float) @fun.register(tuple) def _(text): print('float, tuple') fun('i am is hubo') fun(123) fun(['a','b','c']) fun(1.23) print(fun.registry) # 所有的泛型函數(shù) print(fun.registry[int]) # 獲取int的泛型函數(shù) # String:i am is hubo # 123 # 0 a # 1 b # 2 c # float, tuple # {<class 'object'>: <function fun at 0x106d10f28>, <class 'int'>: <function _ at 0x106f0b9d8>, <class 'list'>: <function _ at 0x106f0ba60>, <class 'tuple'>: <function _ at 0x106f0bb70>, <class 'float'>: <function _ at 0x106f0bb70>} # <function _ at 0x106f0b9d8>
8. singledispatchmethod
與泛型函數(shù)類似,可以編寫一個(gè)使用不同類型的參數(shù)調(diào)用的泛型方法聲明,根據(jù)傳遞給通用方法的參數(shù)的類型,編譯器會(huì)適當(dāng)?shù)靥幚砻總€(gè)方法調(diào)用。?
class Negator: @singledispatchmethod def neg(self, arg): raise NotImplementedError("Cannot negate a") @neg.register def _(self, arg: int): return -arg @neg.register def _(self, arg: bool): return not arg
9. total_ordering
它是針對某個(gè)類如果定義了lt、le、gt、ge這些方法中的至少一個(gè),使用該裝飾器,則會(huì)自動(dòng)的把其他幾個(gè)比較函數(shù)也實(shí)現(xiàn)在該類中?
from functools import total_ordering class Person: # 定義相等的比較函數(shù) def __eq__(self,other): return ((self.lastname.lower(),self.firstname.lower()) == (other.lastname.lower(),other.firstname.lower())) # 定義小于的比較函數(shù) def __lt__(self,other): return ((self.lastname.lower(),self.firstname.lower()) < (other.lastname.lower(),other.firstname.lower())) p1 = Person() p2 = Person() p1.lastname = "123" p1.firstname = "000" p2.lastname = "1231" p2.firstname = "000" print p1 < p2 # True print p1 <= p2 # True print p1 == p2 # False print p1 > p2 # False print p1 >= p2 # False
10. update_wrapper
使用 partial
包裝的函數(shù)是沒有__name__
和__doc__
屬性的。update_wrapper
作用:將被包裝函數(shù)的__name__
等屬性,拷貝到新的函數(shù)中去。?
from functools import update_wrapper def wrap2(func): def inner(*args): return func(*args) return update_wrapper(inner, func) @wrap2 def demo(): print('hello world') print(demo.__name__) # demo
11. wraps
warps
函數(shù)是為了在裝飾器拷貝被裝飾函數(shù)的__name__
。
就是在update_wrapper
上進(jìn)行一個(gè)包裝?
from functools import wraps def wrap1(func): @wraps(func) # 去掉就會(huì)返回inner def inner(*args): print(func.__name__) return func(*args) return inner @wrap1 def demo(): print('hello world') print(demo.__name__) # demo
參考文獻(xiàn)
Python-functools詳解
Python的functools模塊
Python的Functools模塊簡介_函數(shù)
cached_property/緩存屬性
蓋若 gairuo.com
原文鏈接:https://blog.csdn.net/xhtchina/article/details/128487830
相關(guān)推薦
- 2022-08-04 GoFrame框架gset交差并補(bǔ)集使用實(shí)例_Golang
- 2022-05-22 Nginx基礎(chǔ)location語法及功能配置實(shí)例_nginx
- 2022-03-24 android?Launcher?AppWidget添加步驟介紹_Android
- 2023-04-24 React組件與事件的創(chuàng)建使用教程_React
- 2022-08-15 gin框架中使用websocket發(fā)送消息及群聊
- 2022-03-24 c++中的bind使用方法_C 語言
- 2023-01-10 C#固定大小緩沖區(qū)及使用指針復(fù)制數(shù)據(jù)詳解_C#教程
- 2022-06-09 Python中re模塊的元字符使用小結(jié)_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支