Python functools模塊學(xué)習(xí)總結(jié)
文檔 地址
functools.partial
作用:
functools.partial 通過包裝手法,允許我們 "重新定義" 函數(shù)簽名
用一些默認(rèn)參數(shù)包裝一個(gè)可調(diào)用對(duì)象,返回結(jié)果是可調(diào)用對(duì)象,并且可以像原始對(duì)象一樣對(duì)待
凍結(jié)部分函數(shù)位置函數(shù)或關(guān)鍵字參數(shù),簡(jiǎn)化函數(shù),更少更靈活的函數(shù)參數(shù)調(diào)用
#args/keywords 調(diào)用partial時(shí)參數(shù)
def partial(func, *args, **keywords):
def newfunc(*fargs, **fkeywords):
newkeywords = keywords.copy()
newkeywords.update(fkeywords)
return func(*(args + fargs), **newkeywords) #合并,調(diào)用原始函數(shù),此時(shí)用了partial的參數(shù)
newfunc.func = func
newfunc.args = args
newfunc.keywords = keywords
return newfunc
聲明:
urlunquote = functools.partial(urlunquote, encoding='latin1')
當(dāng)調(diào)用 urlunquote(args, *kargs)
相當(dāng)于 urlunquote(args, *kargs, encoding='latin1')
E.g:
import functools
def add(a, b):
return a + b
add(4, 2)
6
plus3 = functools.partial(add, 3)
plus5 = functools.partial(add, 5)
plus3(4)
7
plus3(7)
10
plus5(10)
15
應(yīng)用:
典型的,函數(shù)在執(zhí)行時(shí),要帶上所有必要的參數(shù)進(jìn)行調(diào)用。
然后,有時(shí)參數(shù)可以在函數(shù)被調(diào)用之前提前獲知。
這種情況下,一個(gè)函數(shù)有一個(gè)或多個(gè)參數(shù)預(yù)先就能用上,以便函數(shù)能用更少的參數(shù)進(jìn)行調(diào)用。
functool.update_wrapper
默認(rèn)partial對(duì)象沒有__name__和__doc__, 這種情況下,對(duì)于裝飾器函數(shù)非常難以debug.使用update_wrapper(),從原始對(duì)象拷貝或加入現(xiàn)有partial對(duì)象
它可以把被封裝函數(shù)的__name__、module、__doc__和 __dict__都復(fù)制到封裝函數(shù)去(模塊級(jí)別常量WRAPPER_ASSIGNMENTS, WRAPPER_UPDATES)
>>> functools.WRAPPER_ASSIGNMENTS
('__module__', '__name__', '__doc__')
>>> functools.WRAPPER_UPDATES
('__dict__',)
這個(gè)函數(shù)主要用在裝飾器函數(shù)中,裝飾器返回函數(shù)反射得到的是包裝函數(shù)的函數(shù)定義而不是原始函數(shù)定義
#!/usr/bin/env python
# encoding: utf-8
def wrap(func):
def call_it(*args, **kwargs):
"""wrap func: call_it"""
print 'before call'
return func(*args, **kwargs)
return call_it
@wrap
def hello():
"""say hello"""
print 'hello world'
from functools import update_wrapper
def wrap2(func):
def call_it(*args, **kwargs):
"""wrap func: call_it2"""
print 'before call'
return func(*args, **kwargs)
return update_wrapper(call_it, func)
@wrap2
def hello2():
"""test hello"""
print 'hello world2'
if __name__ == '__main__':
hello()
print hello.__name__
print hello.__doc__
print
hello2()
print hello2.__name__
print hello2.__doc__
得到結(jié)果:
before call
hello world
call_it
wrap func: call_it
before call
hello world2
hello2
test hello
functool.wraps
調(diào)用函數(shù)裝飾器partial(update_wrapper, wrapped=wrapped, assigned=assigned, updated=updated)的簡(jiǎn)寫
from functools import wraps
def wrap3(func):
@wraps(func)
def call_it(*args, **kwargs):
"""wrap func: call_it2"""
print 'before call'
return func(*args, **kwargs)
return call_it
@wrap3
def hello3():
"""test hello 3"""
print 'hello world3'
結(jié)果
before call
hello world3
hello3
test hello 3
functools.reduce
functools.reduce(function, iterable[, initializer])
等同于內(nèi)置函數(shù)reduce()
用這個(gè)的原因是使代碼更兼容(python3)
functools.cmp_to_key
functools.cmp_to_key(func)
將老式鼻尖函數(shù)轉(zhuǎn)換成key函數(shù),用在接受key函數(shù)的方法中(such as sorted(), min(), max(), heapq.nlargest(), heapq.nsmallest(), itertools.groupby())
一個(gè)比較函數(shù),接收兩個(gè)參數(shù),小于,返回負(fù)數(shù),等于,返回0,大于返回整數(shù)
key函數(shù),接收一個(gè)參數(shù),返回一個(gè)表明該參數(shù)在期望序列中的位置
例如:
sorted(iterable, key=cmp_to_key(locale.strcoll)) # locale-aware sort order
functools.total_ordering
functools.total_ordering(cls)
這個(gè)裝飾器是在python2.7的時(shí)候加上的,它是針對(duì)某個(gè)類如果定義了__lt__、le、gt、__ge__這些方法中的至少一個(gè),使用該裝飾器,則會(huì)自動(dòng)的把其他幾個(gè)比較函數(shù)也實(shí)現(xià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()))
print dir(Student)
得到
['__doc__', '__eq__', '__ge__', '__gt__', '__le__', '__lt__', '__module__']
- Python中functools模塊的常用函數(shù)解析
- Python中functools模塊函數(shù)解析
- Python使用functools實(shí)現(xiàn)注解同步方法
- Python3標(biāo)準(zhǔn)庫(kù)之functools管理函數(shù)的工具詳解
- Python編程functools模塊創(chuàng)建修改的高階函數(shù)解析
- Python的functools模塊使用及說明
- Python庫(kù)functools示例詳解
- Python中的functools partial詳解
- python高階函數(shù)functools模塊的具體使用
- Python中Functools模塊的高級(jí)操作詳解
- Python函數(shù)式編程模塊functools的使用與實(shí)踐
相關(guān)文章
如何將yolo格式轉(zhuǎn)化為voc格式:txt轉(zhuǎn)xml(親測(cè)有效)
這篇文章主要介紹了如何將yolo格式轉(zhuǎn)化為voc格式:txt轉(zhuǎn)xml,親測(cè)有效,可以使用,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),感興趣的朋友參考下吧2023-12-12
Django model反向關(guān)聯(lián)名稱的方法
今天小編就為大家分享一篇Django model反向關(guān)聯(lián)名稱的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-12-12
python GUI庫(kù)圖形界面開發(fā)之PyQt5 UI主線程與耗時(shí)線程分離詳細(xì)方法實(shí)例
這篇文章主要介紹了python GUI庫(kù)圖形界面開發(fā)之PyQt5 UI主線程與耗時(shí)線程分離詳細(xì)方法實(shí)例,需要的朋友可以參考下2020-02-02
python實(shí)現(xiàn)大戰(zhàn)外星人小游戲?qū)嵗a
這篇文章主要介紹了python實(shí)現(xiàn)大戰(zhàn)外星人小游戲,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12
Python實(shí)現(xiàn)批量將word轉(zhuǎn)html并將html內(nèi)容發(fā)布至網(wǎng)站的方法
這篇文章主要介紹了Python實(shí)現(xiàn)批量將word轉(zhuǎn)html并將html內(nèi)容發(fā)布至網(wǎng)站的方法,涉及Python調(diào)用第三方接口進(jìn)行文件轉(zhuǎn)換及操作數(shù)據(jù)庫(kù)等相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
探索Python數(shù)據(jù)可視化庫(kù)中Plotly Express的使用方法
在數(shù)據(jù)分析和可視化領(lǐng)域,數(shù)據(jù)的有效呈現(xiàn)是至關(guān)重要的,python作為一種強(qiáng)大的編程語言,提供了多種數(shù)據(jù)可視化工具和庫(kù),本文將介紹Plotly Express的基本概念和使用方法,幫助讀者快速入門并掌握數(shù)據(jù)可視化的技巧2023-06-06
使用pandas實(shí)現(xiàn)csv/excel sheet互相轉(zhuǎn)換的方法
今天小編就為大家分享一篇使用pandas實(shí)現(xiàn)csv/excel sheet互相轉(zhuǎn)換的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-12-12
python基礎(chǔ)學(xué)習(xí)之如何對(duì)元組各個(gè)元素進(jìn)行命名詳解
python的元祖和列表類似,不同之處在于元祖的元素不能修改,下面這篇文章主要給大家介紹了關(guān)于python基礎(chǔ)學(xué)習(xí)之如何對(duì)元組各個(gè)元素進(jìn)行命名的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-07-07
LRUCache的實(shí)現(xiàn)原理及利用python實(shí)現(xiàn)的方法
LruCache 是 Android 的一個(gè)內(nèi)部類,提供了基于內(nèi)存實(shí)現(xiàn)的緩存,而下面這篇文章主要給大家介紹了關(guān)于LRUCache的實(shí)現(xiàn)原理以及利用python實(shí)現(xiàn)的方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-11-11
python基于Pandas讀寫MySQL數(shù)據(jù)庫(kù)
這篇文章主要介紹了python基于Pandas讀寫MySQL數(shù)據(jù)庫(kù),幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-04-04

