Python的functools模塊使用及說(shuō)明
partial
用于創(chuàng)建一個(gè)偏函數(shù),將默認(rèn)參數(shù)包裝一個(gè)可調(diào)用對(duì)象,返回結(jié)果也是可調(diào)用對(duì)象。
偏函數(shù)可以固定住原函數(shù)的部分參數(shù),從而在調(diào)用時(shí)更簡(jiǎn)單。
from functools import partial
int2 = partial(int, base=8)
print(int2('123'))
# 83update_wrapper
使用 partial 包裝的函數(shù)是沒(méi)有__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__)
# demowraps
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__)
# demoreduce
在 Python2 中等同于內(nèi)建函數(shù) 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
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']
Python3 之前還提供了cmp參數(shù)來(lái)比較兩個(gè)元素
cmp_to_key 函數(shù)就是用來(lái)將老式的比較函數(shù)轉(zhuǎn)化為 key 函數(shù)
lru_cache
允許我們將一個(gè)函數(shù)的返回值快速地緩存或取消緩存。
該裝飾器用于緩存函數(shù)的調(diào)用結(jié)果,對(duì)于需要多次調(diào)用的函數(shù),而且每次調(diào)用參數(shù)都相同,則可以用該裝飾器緩存調(diào)用結(jié)果,從而加快程序運(yùn)行。
該裝飾器會(huì)將不同的調(diào)用結(jié)果緩存在內(nèi)存中,因此需要注意內(nèi)存占用問(wèn)題。
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() ? # 清空緩存
singledispatch
單分發(fā)器, Python3.4新增,用于實(shí)現(xiàn)泛型函數(shù)。
根據(jù)單一參數(shù)的類型來(lái)判斷調(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>以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Python functools模塊學(xué)習(xí)總結(jié)
- 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庫(kù)functools示例詳解
- Python中的functools partial詳解
- python高階函數(shù)functools模塊的具體使用
- Python中Functools模塊的高級(jí)操作詳解
- Python函數(shù)式編程模塊functools的使用與實(shí)踐
相關(guān)文章
Python?Nuitka打包的實(shí)現(xiàn)步驟
在Python應(yīng)用程序開(kāi)發(fā)中,打包是將代碼和依賴項(xiàng)組合成可執(zhí)行文件或庫(kù)的關(guān)鍵步驟之一,本文主要介紹了Python?Nuitka打包的實(shí)現(xiàn)步驟,感興趣的可以了解一下2023-12-12
零基礎(chǔ)寫(xiě)python爬蟲(chóng)之使用Scrapy框架編寫(xiě)爬蟲(chóng)
前面的文章我們介紹了Python爬蟲(chóng)框架Scrapy的安裝與配置等基本資料,本文我們就來(lái)看看如何使用Scrapy框架方便快捷的抓取一個(gè)網(wǎng)站的內(nèi)容,隨便選個(gè)小站(dmoz.org)來(lái)示例吧2014-11-11
Python3.4 tkinter,PIL圖片轉(zhuǎn)換
我們給大家整理了關(guān)于Python3.4 tkinter,PIL圖片轉(zhuǎn)換的相關(guān)完整代碼,大家可以學(xué)習(xí)測(cè)試下。2018-06-06

