import functools
'''
functools.cmp_to_key(func)
將老式的比較函數(shù)(func)轉(zhuǎn)換為關(guān)鍵字函數(shù)(key function)。在 Python3中比較大小、排序都是基于關(guān)鍵字函數(shù)的,
Python 3 不支持老式的比較函數(shù)。
'''
class Person(object):
def __init__(self, name):
self.name = name
def __repr__(self):
return f"My name is {self.name}"
def compare(p1, p2):
return len(p1.name) - len(p2.name)
data = [Person('張三'), Person('趙飛燕'), Person('漢武大帝'), Person('松下幸之助'), Person('愛新覺羅玄燁'), Person('阿爾伯特愛因斯坦')]
data.sort(key=functools.cmp_to_key(compare))
print(data)
# [My name is 張三, My name is 趙飛燕, My name is 漢武大帝, My name is 松下幸之助, My name is 愛新覺羅玄燁, My name is 阿爾伯特愛因斯坦]
'''
functools.reduce(function, iterable[, initializer])
將初始值(默認(rèn)為 0,可由 initializer 參數(shù)指定)、迭代器的當(dāng)前元素傳入 function 函數(shù),將計(jì)算出來的函數(shù)結(jié)果作為下一次計(jì)算的初始值、
迭代器的下一個(gè)元素再次調(diào)用 function 函數(shù)……依此類推,直到迭代器的最后一個(gè)元素。
'''
print(functools.reduce(lambda x, y: x + y, range(10), 100))
'''
@functools.lru_cache(maxsize=128, typed=False)
該函數(shù)裝飾器使用 LRU(最近最少使用)緩存算法來緩存相對耗時(shí)的函數(shù)結(jié)果,避免傳入相同的參數(shù)重復(fù)計(jì)算。同時(shí),緩存并不會(huì)無限增長,
不用的緩存會(huì)被釋放。其中 maxsize 參數(shù)用于設(shè)置緩存占用的最大字節(jié)數(shù),typed 參數(shù)用于設(shè)置將不同類型的緩存結(jié)果分開存放。
'''
@functools.lru_cache(maxsize=128)
def fibnacci(n):
if n in (1, 2):
return 1
else:
return fibnacci(n - 1) + fibnacci(n - 2)
print(fibnacci(100))
'''
functools.partial(func, *args, **keywords)
該函數(shù)用于為func函數(shù)的部分參數(shù)指定參數(shù)值,從而得到一個(gè)轉(zhuǎn)換后的函數(shù),程序以后調(diào)用轉(zhuǎn)換后的函數(shù)時(shí),就可以少傳入那些己指定值的參數(shù)。
'''
def add(x, y):
return x + y
par_func = functools.partial(add, 10)
print(par_func(5)) # 15
'''
partialmethod() 與 partial() 函數(shù)的作用基本相似,區(qū)別只是 partial() 函數(shù)用于為函數(shù)的部分參數(shù)綁定值;
而 partialmethod() 函數(shù)則用于為類中方法的部分參數(shù)綁定值。如下程序示范了 partialmethod() 函數(shù)的用法:
'''
class Person(object):
def __init__(self):
self._age = 18
@property
def age(self):
return self._age
def set_age(self, number: int):
if not isinstance(number, int):
raise ValueError('int needed')
if number > 60:
self._age = 100
elif number < 10:
self._age = 5
else:
self._age = 18
set_young = functools.partialmethod(set_age, 8)
set_old = functools.partialmethod(set_age, 70)
p = Person()
print(p.age) # 18
p.set_young() # 相當(dāng)于調(diào)用set_age(8)
print(p.age) # 5
p.set_old() # 相當(dāng)于調(diào)用set_age(70)
print(p.age) # 100
'''
@functools.total_ordering
這個(gè)類裝飾器(作用類似于函數(shù)裝飾器,只是它用于修飾類)用于為類自動(dòng)生成比較方法。通常來說,開發(fā)者只要提供
__lt__()、__le__()、__gt__()、__ge__() 其中之一(最好能提供 __eq__() 方法),@functools.total_ordering裝飾器就會(huì)
為該類生成剩下的比較方法。
'''
@functools.total_ordering
class Person(object):
def __init__(self, age):
self.age = age
def __eq__(self, other):
return self.age == other.age
def __gt__(self, other):
if self.age > other.age:
return True
return False
p1 = Person(18)
p2 = Person(20)
print(p1 > p2) # False
print(p1 >= p2) # False
print(p1 < p2) # True
print(p1 <= p2) # True
print(p1 == p2) # False
'''
@functools.singledispatch
該函數(shù)裝飾器用于實(shí)現(xiàn)函數(shù)對多個(gè)類型進(jìn)行重載。比如同樣的函數(shù)名稱,為不同的參數(shù)類型提供不同的功能實(shí)現(xiàn).
該函數(shù)的本質(zhì)就是根據(jù)參數(shù)類型的變換,將函數(shù)轉(zhuǎn)向調(diào)用不同的函數(shù)。
'''
@functools.singledispatch
def test(args):
print(args)
@test.register(int)
def test_int(args):
print('the args is int')
@test.register(float)
def test_float(args):
print('the args is float')
@test.register(list)
def test_list(args):
print('the args is list')
@test.register(dict)
@test.register(set)
def test_dict_set(args):
print('the args is dict or set')
test(1) # the args is int
test(1.0) #the args is float
test(list(range(5))) # the args is list
test(dict([(1, 2), (3, 4)])) # the args is dict or set
print(test.registry.keys()) # 獲取test函數(shù)所綁定的全部類型
print(test.registry[int]) # 獲取test函數(shù)綁定的int類型的函數(shù)
'''
@functools.wraps(wrapped, assigned=WRAPPER_ASSIGNMENTS, updated=WRAPPER_UPDATES)
該函數(shù)裝飾器用于修飾包裝函數(shù),使包裝函數(shù)看上去就像 wrapped 函數(shù)。
'''
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
return result
return wrapper
@decorator
def test():
"""我是test函數(shù)"""
print('test is calling')
test() # test is calling
print(test.__name__) # test,不加裝飾器會(huì)顯示為wrapper
print(test.__doc__) # 我是test函數(shù),不加裝飾器得不到說明文檔
'''
functools.update_wrapper和functools.wraps的區(qū)別在于,functools.update_wrapper是一個(gè)函數(shù),它的第一個(gè)參數(shù)是包裝函數(shù),第二個(gè)參數(shù)是
被包裝函數(shù)。
'''
def decorator(func):
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
functools.update_wrapper(wrapper, func)
return result
return wrapper
@decorator
def test():
"""我是test函數(shù)"""
print('test is calling')
test()
print(test.__name__)
print(test.__doc__)
python基礎(chǔ)模塊---functools
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 一、math 模塊、cmath 模塊 Python 中數(shù)學(xué)運(yùn)算常用的函數(shù)基本都在 math 模塊、cmath 模塊...
- 1 os.name #顯示當(dāng)前使用的平臺(tái) 2 os.getcwd() #顯示當(dāng)前python腳本工作路徑 ...
- 安裝第三方模塊: 第一個(gè)方法,cmd內(nèi)直接pip install 模塊名 第二個(gè),網(wǎng)絡(luò)直接下載whl文件 網(wǎng)址:h...
- -一、導(dǎo)入模塊os ,import os 二、os模塊的作用: python的os模塊包含普通的操作系統(tǒng),一般我是...