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()
語法:
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()
語法:
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()
語法:
functools.reduce(function, iterable[, initializer])
該函數(shù)與 Python 內(nèi)置的 reduce() 函數(shù)相同,主要用于編寫兼容 Python 3 的代碼。
functools.partial()
語法:
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ù)更少、使用更簡單的函數(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()
語法:
functools.update_wrapper(wrapper, wrapped[, assigned][, updated])
該函數(shù)用于更新包裝函數(shù)(wrapper),使它看起來像原函數(shù)一樣??蛇x的參數(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ù)之前。如果沒有對(duì)包裝函數(shù)進(jìn)行更新,那么被裝飾后的函數(shù)所具有的元信息就會(huì)變?yōu)榘b函數(shù)的元信息,而不是原函數(shù)的元信息。
functools.wraps()
語法:
functools.wraps(wrapped[, assigned][, updated])
wraps() 簡化了 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() 的說明文檔(docstring)就會(huì)丟失。
- Python函數(shù)式編程模塊functools的使用與實(shí)踐
- Python中Functools模塊的高級(jí)操作詳解
- python常用模塊(math itertools functools sys shutil)使用講解
- python高階函數(shù)functools模塊的具體使用
- Python的functools模塊使用及說明
- Python編程functools模塊創(chuàng)建修改的高階函數(shù)解析
- Python使用functools模塊中的partial函數(shù)生成偏函數(shù)
- Python中functools模塊的常用函數(shù)解析
- Python functools模塊學(xué)習(xí)總結(jié)
- Python中functools 模塊的使用小結(jié)
相關(guān)文章
python 還原梯度下降算法實(shí)現(xiàn)一維線性回歸
這篇文章主要介紹了python 還原梯度下降算法實(shí)現(xiàn)一維線性回歸,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
python通過Matplotlib繪制常見的幾種圖形(推薦)
這篇文章主要介紹了使用matplotlib對(duì)幾種常見的圖形進(jìn)行繪制方法的相關(guān)資料,需要的朋友可以參考下2021-08-08
詳解Python二維數(shù)組與三維數(shù)組切片的方法
這篇文章主要介紹了詳解Python二維數(shù)組與三維數(shù)組切片的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
Python實(shí)現(xiàn)刪除Android工程中的冗余字符串
這篇文章主要介紹了Python實(shí)現(xiàn)刪除Android工程中的冗余字符串,本文實(shí)現(xiàn)的是刪除Android資源(語言)國際化機(jī)制中的一些冗余字符串,需要的朋友可以參考下2015-01-01
Python實(shí)現(xiàn)新型冠狀病毒傳播模型及預(yù)測代碼實(shí)例
在本篇文章里小編給大家整理的是關(guān)于Python實(shí)現(xiàn)新型冠狀病毒傳播模型及預(yù)測代碼內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2020-02-02
Python?tracemalloc跟蹤內(nèi)存分配問題
這篇文章主要介紹了Python?tracemalloc跟蹤內(nèi)存分配問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
python批量設(shè)置多個(gè)Excel文件頁眉頁腳的腳本
這篇文章主要介紹了python批量設(shè)置多個(gè)Excel文件頁眉頁腳的源碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03

