Python中functools模塊函數(shù)解析
Python自帶的 functools 模塊提供了一些常用的高階函數(shù),也就是用于處理其它函數(shù)的特殊函數(shù)。換言之,就是能使用該模塊對(duì)可調(diào)用對(duì)象進(jìn)行處理。
functools模塊函數(shù)概覽
- functools.cmp_to_key(func)
- functools.total_ordering(cls)
- functools.reduce(function, iterable[, initializer])
- functools.partial(func[, args][, *keywords])
- functools.update_wrapper(wrapper, wrapped[, assigned][, updated])
- functools.wraps(wrapped[, assigned][, updated])
functools.cmp_to_key()
語(yǔ)法:
functools.cmp_to_key(func)
該函數(shù)用于將舊式的比較函數(shù)轉(zhuǎn)換為關(guān)鍵字函數(shù)。
舊式的比較函數(shù):接收兩個(gè)參數(shù),返回比較的結(jié)果。返回值小于零則前者小于后者,返回值大于零則相反,返回值等于零則兩者相等。
關(guān)鍵字函數(shù):接收一個(gè)參數(shù),返回其對(duì)應(yīng)的可比較對(duì)象。例如 sorted(), min(), max(), heapq.nlargest(), heapq.nsmallest(), itertools.groupby() 都可作為關(guān)鍵字函數(shù)。
在 Python 3 中,有很多地方都不再支持舊式的比較函數(shù),此時(shí)可以使用 cmp_to_key() 進(jìn)行轉(zhuǎn)換。
示例:
sorted(iterable, key=cmp_to_key(cmp_func))
functools.total_ordering()
語(yǔ)法:
functools.total_ordering(cls)
這是一個(gè)類裝飾器,用于自動(dòng)實(shí)現(xiàn)類的比較運(yùn)算。
我們只需要在類中實(shí)現(xiàn) __eq__() 方法和以下方法中的任意一個(gè) __lt__(), __le__(), __gt__(), __ge__(),那么 total_ordering() 就能自動(dòng)幫我們實(shí)現(xiàn)余下的幾種比較運(yùn)算。
示例:
@total_ordering class Student: def __eq__(self, other): return ((self.lastname.lower(), self.firstname.lower()) == (other.lastname.lower(), other.firstname.lower())) def __lt__(self, other): return ((self.lastname.lower(), self.firstname.lower()) < (other.lastname.lower(), other.firstname.lower()))
functools.reduce()
語(yǔ)法:
functools.reduce(function, iterable[, initializer])
該函數(shù)與 Python 內(nèi)置的 reduce() 函數(shù)相同,主要用于編寫兼容 Python 3 的代碼。
functools.partial()
語(yǔ)法:
functools.partial(func[, *args][, **keywords])
該函數(shù)返回一個(gè) partial 對(duì)象,調(diào)用該對(duì)象的效果相當(dāng)于調(diào)用 func 函數(shù),并傳入位置參數(shù) args 和關(guān)鍵字參數(shù) keywords 。如果調(diào)用該對(duì)象時(shí)傳入了位置參數(shù),則這些參數(shù)會(huì)被添加到 args 中。如果傳入了關(guān)鍵字參數(shù),則會(huì)被添加到 keywords 中。
partial() 函數(shù)的等價(jià)實(shí)現(xiàn)大致如下:
def partial(func, *args, **keywords): def newfunc(*fargs, **fkeywords): newkeywords = keywords.copy() newkeywords.update(fkeywords) return func(*(args + fargs), **newkeywords) newfunc.func = func newfunc.args = args newfunc.keywords = keywords return newfunc
partial() 函數(shù)主要用于“凍結(jié)”某個(gè)函數(shù)的部分參數(shù),返回一個(gè)參數(shù)更少、使用更簡(jiǎn)單的函數(shù)對(duì)象。
示例:
>>> from functools import partial >>> basetwo = partial(int, base=2) >>> basetwo.__doc__ = 'Convert base 2 string to an int.' >>> basetwo('10010') 18
functools.update_wrapper()
語(yǔ)法:
functools.update_wrapper(wrapper, wrapped[, assigned][, updated])
該函數(shù)用于更新包裝函數(shù)(wrapper),使它看起來(lái)像原函數(shù)一樣。可選的參數(shù)是一個(gè)元組,assigned 元組指定要直接使用原函數(shù)的值進(jìn)行替換的屬性,updated 元組指定要對(duì)照原函數(shù)進(jìn)行更新的屬性。這兩個(gè)參數(shù)的默認(rèn)值分別是模塊級(jí)別的常量:WRAPPER_ASSIGNMENTS 和 WRAPPER_UPDATES。前者指定了對(duì)包裝函數(shù)的 __name__, __module__, __doc__ 屬性進(jìn)行直接賦值,而后者指定了對(duì)包裝函數(shù)的 __dict__ 屬性進(jìn)行更新。
該函數(shù)主要用于裝飾器函數(shù)的定義中,置于包裝函數(shù)之前。如果沒(méi)有對(duì)包裝函數(shù)進(jìn)行更新,那么被裝飾后的函數(shù)所具有的元信息就會(huì)變?yōu)榘b函數(shù)的元信息,而不是原函數(shù)的元信息。
functools.wraps()
語(yǔ)法:
functools.wraps(wrapped[, assigned][, updated])
wraps() 簡(jiǎn)化了 update_wrapper() 函數(shù)的調(diào)用。它等價(jià)于 partial(update_wrapper, wrapped=wrapped, assigned, updated=updated)。
示例:
>>> from functools import wraps >>> def my_decorator(f): ... @wraps(f) ... def wrapper(*args, **kwds): ... print 'Calling decorated function' ... return f(*args, **kwds) ... return wrapper >>> @my_decorator ... def example(): ... """Docstring""" ... print 'Called example function' >>> example() Calling decorated function Called example function >>> example.__name__ 'example' >>> example.__doc__ 'Docstring'
如果不使用這個(gè)函數(shù),示例中的函數(shù)名就會(huì)變成 wrapper ,并且原函數(shù) example() 的說(shuō)明文檔(docstring)就會(huì)丟失。
- Python functools模塊學(xué)習(xí)總結(jié)
- Python中functools模塊的常用函數(shù)解析
- Python使用functools實(shí)現(xiàn)注解同步方法
- Python3標(biāo)準(zhǔn)庫(kù)之functools管理函數(shù)的工具詳解
- Python編程functools模塊創(chuàng)建修改的高階函數(shù)解析
- Python的functools模塊使用及說(shuō)明
- Python庫(kù)functools示例詳解
- Python中的functools partial詳解
- python高階函數(shù)functools模塊的具體使用
- Python中Functools模塊的高級(jí)操作詳解
- Python函數(shù)式編程模塊functools的使用與實(shí)踐
相關(guān)文章
解決pymysql cursor.fetchall() 獲取不到數(shù)據(jù)的問(wèn)題
這篇文章主要介紹了解決pymysql cursor.fetchall() 獲取不到數(shù)據(jù)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05PythonPC客戶端自動(dòng)化實(shí)現(xiàn)原理(pywinauto)
這篇文章主要介紹了Python基于pywinauto實(shí)現(xiàn)PC客戶端自動(dòng)化,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05python實(shí)現(xiàn)requests發(fā)送/上傳多個(gè)文件的示例
今天小編就為大家分享一篇python實(shí)現(xiàn)requests發(fā)送/上傳多個(gè)文件的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06深入理解Python虛擬機(jī)中描述器的實(shí)現(xiàn)原理
這篇文章主要給大家介紹一個(gè)我們?cè)谑褂妙惖臅r(shí)候經(jīng)常使用但是卻很少在意的黑科技——描述器的實(shí)現(xiàn)原理,文中的示例代碼講解詳細(xì),需要的可以參考一下2023-05-05Python button選取本地圖片并顯示的實(shí)例
今天小編就為大家分享一篇Python button選取本地圖片并顯示的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06Python實(shí)現(xiàn)簡(jiǎn)單狀態(tài)框架的方法
這篇文章主要介紹了Python實(shí)現(xiàn)簡(jiǎn)單狀態(tài)框架的方法,涉及Python狀態(tài)框架的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03Python+Pygame實(shí)戰(zhàn)之文字劇情游戲的實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了如何利用Python和Pygame實(shí)現(xiàn)兩款文字劇情游戲——《巨龍之洞》和《太空礦工》,感興趣的小伙伴可以了解一下2022-12-12Django如何簡(jiǎn)單快速實(shí)現(xiàn)PUT、DELETE方法
這篇文章主要介紹了Django如何簡(jiǎn)單快速實(shí)現(xiàn)PUT、DELETE方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07