Python3標(biāo)準(zhǔn)庫之functools管理函數(shù)的工具詳解
1. functools管理函數(shù)的工具
functools模塊提供了一些工具來調(diào)整或擴展函數(shù)和其他callable對象,從而不必完全重寫。
1.1 修飾符
functools模塊提供的主要工具就是partial類,可以用來“包裝”一個有默認參數(shù)的callable對象。得到的對象本身就是callable,可以把它看作是原來的函數(shù)。它與原函數(shù)的參數(shù)完全相同,調(diào)用時還可以提供額外的位置或命名函數(shù)。可以使用partial而不是lambda為函數(shù)提供默認參數(shù),有些參數(shù)可以不指定。
1.1.1 部分對象
第一個例子顯示了函數(shù)myfunc()的兩個簡單partial對象。show_details()的輸出中包含這個部分對象(partial object)的func、args和keywords屬性。
import functools
def myfunc(a, b=2):
"Docstring for myfunc()."
print(' called myfunc with:', (a, b))
def show_details(name, f, is_partial=False):
"Show details of a callable object."
print('{}:'.format(name))
print(' object:', f)
if not is_partial:
print(' __name__:', f.__name__)
if is_partial:
print(' func:', f.func)
print(' args:', f.args)
print(' keywords:', f.keywords)
return
show_details('myfunc', myfunc)
myfunc('a', 3)
print()
# Set a different default value for 'b', but require
# the caller to provide 'a'.
p1 = functools.partial(myfunc, b=4)
show_details('partial with named default', p1, True)
p1('passing a')
p1('override b', b=5)
print()
# Set default values for both 'a' and 'b'.
p2 = functools.partial(myfunc, 'default a', b=99)
show_details('partial with defaults', p2, True)
p2()
p2(b='override b')
print()
print('Insufficient arguments:')
p1()
在這個例子的最后,調(diào)用了之前創(chuàng)建的第一個partial,但沒有為a傳入一個值,這便會導(dǎo)致一個異常。
1.1.2 獲取函數(shù)屬性
默認的,partial對象沒有__name__或__doc__屬性。如果沒有這些屬性,被修飾的函數(shù)將更難調(diào)試。使用update_wrapper()可以從原函數(shù)將屬性復(fù)制或增加到partial對象。
import functools
def myfunc(a, b=2):
"Docstring for myfunc()."
print(' called myfunc with:', (a, b))
def show_details(name, f):
"Show details of a callable object."
print('{}:'.format(name))
print(' object:', f)
print(' __name__:', end=' ')
try:
print(f.__name__)
except AttributeError:
print('(no __name__)')
print(' __doc__', repr(f.__doc__))
print()
show_details('myfunc', myfunc)
p1 = functools.partial(myfunc, b=4)
show_details('raw wrapper', p1)
print('Updating wrapper:')
print(' assign:', functools.WRAPPER_ASSIGNMENTS)
print(' update:', functools.WRAPPER_UPDATES)
print()
functools.update_wrapper(p1, myfunc)
show_details('updated wrapper', p1)
增加到包裝器的屬性在WRAPPER_ASSIGNMENTS中定義,另外WARPPER_UPDATES列出了要修改的值。
1.1.3 其他callable
partial適用于任何callable對象,而不只是獨立的函數(shù)。
import functools
class MyClass:
"Demonstration class for functools"
def __call__(self, e, f=6):
"Docstring for MyClass.__call__"
print(' called object with:', (self, e, f))
def show_details(name, f):
"Show details of a callable object."
print('{}:'.format(name))
print(' object:', f)
print(' __name__:', end=' ')
try:
print(f.__name__)
except AttributeError:
print('(no __name__)')
print(' __doc__', repr(f.__doc__))
return
o = MyClass()
show_details('instance', o)
o('e goes here')
print()
p = functools.partial(o, e='default for e', f=8)
functools.update_wrapper(p, o)
show_details('instance wrapper', p)
p()
這個例子從一個包含__call__()方法的類實例中創(chuàng)建部分對象。
1.1.4 方法和函數(shù)
partial()返回一個可以直接使用的callable,partialmethod()返回的callable則可以用作對象的非綁定方法。在下面的例子中,這個獨立函數(shù)兩次被增加為MyClass的屬性,一次使用partialmethod()作為method1(),另一次使用partial()作為method2()。
import functools
def standalone(self, a=1, b=2):
"Standalone function"
print(' called standalone with:', (self, a, b))
if self is not None:
print(' self.attr =', self.attr)
class MyClass:
"Demonstration class for functools"
def __init__(self):
self.attr = 'instance attribute'
method1 = functools.partialmethod(standalone)
method2 = functools.partial(standalone)
o = MyClass()
print('standalone')
standalone(None)
print()
print('method1 as partialmethod')
o.method1()
print()
print('method2 as partial')
try:
o.method2()
except TypeError as err:
print('ERROR: {}'.format(err))
method1()可以從MyClass的一個實例中調(diào)用,這個實例作為第一個參數(shù)傳入,這與采用通常方法定義的方法是一樣的。method2()未被定義為綁定方法,所以必須顯式傳遞self參數(shù);否則,這個調(diào)用會導(dǎo)致TypeError。
1.1.5 獲取修飾符的函數(shù)屬性
更新所包裝callable的屬性對修飾符尤其有用,因為轉(zhuǎn)換后的函數(shù)最后會得到原“裸”函數(shù)的屬性。
import functools
def show_details(name, f):
"Show details of a callable object."
print('{}:'.format(name))
print(' object:', f)
print(' __name__:', end=' ')
try:
print(f.__name__)
except AttributeError:
print('(no __name__)')
print(' __doc__', repr(f.__doc__))
print()
def simple_decorator(f):
@functools.wraps(f)
def decorated(a='decorated defaults', b=1):
print(' decorated:', (a, b))
print(' ', end=' ')
return f(a, b=b)
return decorated
def myfunc(a, b=2):
"myfunc() is not complicated"
print(' myfunc:', (a, b))
return
# The raw function
show_details('myfunc', myfunc)
myfunc('unwrapped, default b')
myfunc('unwrapped, passing b', 3)
print()
# Wrap explicitly
wrapped_myfunc = simple_decorator(myfunc)
show_details('wrapped_myfunc', wrapped_myfunc)
wrapped_myfunc()
wrapped_myfunc('args to wrapped', 4)
print()
# Wrap with decorator syntax
@simple_decorator
def decorated_myfunc(a, b):
myfunc(a, b)
return
show_details('decorated_myfunc', decorated_myfunc)
decorated_myfunc()
decorated_myfunc('args to decorated', 4)
functools提供了一個修飾符wraps(),它會對所修飾的函數(shù)應(yīng)用update_wrapper()。
1.2 比較
在Python 2中,類可以定義一個__cmp__()方法,它會根據(jù)這個對象小于、對于或者大于所比較的元素而分別返回-1、0或1.Python 2.1引入了富比較(rich comparison)方法API(__lt__()、__le__()、__eq__()、__ne__()、__gt__()和__ge__()) ,可以完成一個比較操作并返回一個布爾值。Python 3廢棄了__cmp__()而代之以這些新的方法,另外functools提供了一些工具,從而能更容易地編寫符合新要求的類,即符合Python 3中新的比較需求。
1.2.1 富比較
設(shè)計富比較API是為了支持涉及復(fù)雜比較的類,以最高效的方式實現(xiàn)各個測試。不過,如果比較相對簡單的類,就沒有必要手動地創(chuàng)建各個富比價方法了。total_ordering()類修飾符可以為一個提供了部分方法的類增加其余的方法。
import functools
import inspect
from pprint import pprint
@functools.total_ordering
class MyObject:
def __init__(self, val):
self.val = val
def __eq__(self, other):
print(' testing __eq__({}, {})'.format(
self.val, other.val))
return self.val == other.val
def __gt__(self, other):
print(' testing __gt__({}, {})'.format(
self.val, other.val))
return self.val > other.val
print('Methods:\n')
pprint(inspect.getmembers(MyObject, inspect.isfunction))
a = MyObject(1)
b = MyObject(2)
print('\nComparisons:')
for expr in ['a < b', 'a <= b', 'a == b', 'a >= b', 'a > b']:
print('\n{:<6}:'.format(expr))
result = eval(expr)
print(' result of {}: {}'.format(expr, result))
這個類必須提供__eq__()和另外一個富比較方法的實現(xiàn)。這個修飾符會增加其余方法的實現(xiàn),它們會使用所提供的比較。如果無法完成一個比較,這個方法應(yīng)當(dāng)返回NotImplemented,從而在另一個對象上使用逆比較操作符嘗試比較,如果仍無法比較,便會完全失敗。
1.2.2 比對序
由于Python 3廢棄了老式的比較函數(shù),sort()之類的函數(shù)中也不再支持cmp參數(shù)。對于使用了比較函數(shù)的較老的程序,可以用cmp_to_key()將比較函數(shù)轉(zhuǎn)換為一個返回比較鍵(collation key)的函數(shù),這個鍵用于確定元素在最終序列中的位置。
import functools
class MyObject:
def __init__(self, val):
self.val = val
def __str__(self):
return 'MyObject({})'.format(self.val)
def compare_obj(a, b):
"""Old-style comparison function.
"""
print('comparing {} and {}'.format(a, b))
if a.val < b.val:
return -1
elif a.val > b.val:
return 1
return 0
# Make a key function using cmp_to_key()
get_key = functools.cmp_to_key(compare_obj)
def get_key_wrapper(o):
"Wrapper function for get_key to allow for print statements."
new_key = get_key(o)
print('key_wrapper({}) -> {!r}'.format(o, new_key))
return new_key
objs = [MyObject(x) for x in range(5, 0, -1)]
for o in sorted(objs, key=get_key_wrapper):
print(o)
正常情況下,可以直接使用cmp_to_key(),不過這個例子中引入了一個額外的包裝器函數(shù),這樣調(diào)用鍵函數(shù)時可以打印更多的信息。
如輸出所示,sorted()首先對序列中的每一個元素調(diào)用get_key_wrapper()以生成一個鍵。cmp_to_key()返回的鍵是functools中定義的一個類的實例,這個類使用傳入的老式比較函數(shù)實現(xiàn)富比較API。所有鍵都創(chuàng)建之后,通過比較這些鍵來對序列排序。
1.3 緩存
lru_cache()修飾符將一個函數(shù)包裝在一個“最近最少使用的”緩存中。函數(shù)的參數(shù)用來建立一個散列鍵,然后映射到結(jié)果。后續(xù)的調(diào)用如果有相同的參數(shù),就會從這個緩存獲取值而不會再次調(diào)用函數(shù)。這個修飾符還會為函數(shù)增加方法來檢查緩存的狀態(tài)(cache_info())和清空緩存(cache_clear())。
import functools
@functools.lru_cache()
def expensive(a, b):
print('expensive({}, {})'.format(a, b))
return a * b
MAX = 2
print('First set of calls:')
for i in range(MAX):
for j in range(MAX):
expensive(i, j)
print(expensive.cache_info())
print('\nSecond set of calls:')
for i in range(MAX + 1):
for j in range(MAX + 1):
expensive(i, j)
print(expensive.cache_info())
print('\nClearing cache:')
expensive.cache_clear()
print(expensive.cache_info())
print('\nThird set of calls:')
for i in range(MAX):
for j in range(MAX):
expensive(i, j)
print(expensive.cache_info())
這個例子在一組嵌套循環(huán)中執(zhí)行了多個expensive()調(diào)用。第二次調(diào)用時有相同的參數(shù)值,結(jié)果在緩存中。清空緩存并再次運行循環(huán)時,這些值必須重新計算。
為了避免一個長時間運行的進程導(dǎo)致緩存無限制的擴張,要指定一個最大大小。默認為128個元素,不過對于每個緩存可以用maxsize參數(shù)改變這個大小。
import functools
@functools.lru_cache(maxsize=2)
def expensive(a, b):
print('called expensive({}, {})'.format(a, b))
return a * b
def make_call(a, b):
print('({}, {})'.format(a, b), end=' ')
pre_hits = expensive.cache_info().hits
expensive(a, b)
post_hits = expensive.cache_info().hits
if post_hits > pre_hits:
print('cache hit')
print('Establish the cache')
make_call(1, 2)
make_call(2, 3)
print('\nUse cached items')
make_call(1, 2)
make_call(2, 3)
print('\nCompute a new value, triggering cache expiration')
make_call(3, 4)
print('\nCache still contains one old item')
make_call(2, 3)
print('\nOldest item needs to be recomputed')
make_call(1, 2)
在這個例子中,緩存大小設(shè)置為2個元素。使用第3組不同的參數(shù)(3,4)時,緩存中最老的元素會被清除,代之以這個新結(jié)果。
lru_cache()管理的緩存中鍵必須是可散列的,所以對于用緩存查找包裝的函數(shù),它的所有參數(shù)都必須是可散列的。
import functools
@functools.lru_cache(maxsize=2)
def expensive(a, b):
print('called expensive({}, {})'.format(a, b))
return a * b
def make_call(a, b):
print('({}, {})'.format(a, b), end=' ')
pre_hits = expensive.cache_info().hits
expensive(a, b)
post_hits = expensive.cache_info().hits
if post_hits > pre_hits:
print('cache hit')
make_call(1, 2)
try:
make_call([1], 2)
except TypeError as err:
print('ERROR: {}'.format(err))
try:
make_call(1, {'2': 'two'})
except TypeError as err:
print('ERROR: {}'.format(err))
如果將一個不能散列的對象傳入這個函數(shù),則會產(chǎn)生一個TypeError。
1.4 縮減數(shù)據(jù)集
reduce()函數(shù)取一個callable和一個數(shù)據(jù)序列作為輸入。它會用這個序列中的值調(diào)用這個callable,并累加得到的輸出來生成單個值作為輸出。
import functools
def do_reduce(a, b):
print('do_reduce({}, {})'.format(a, b))
return a + b
data = range(1, 5)
print(data)
result = functools.reduce(do_reduce, data)
print('result: {}'.format(result))
這個例子會累加序列中的數(shù)。
可選的initializer參數(shù)放在序列最前面,像其他元素一樣處理??梢岳眠@個參數(shù)以新輸入更新前面計算的值。
import functools
def do_reduce(a, b):
print('do_reduce({}, {})'.format(a, b))
return a + b
data = range(1, 5)
print(data)
result = functools.reduce(do_reduce, data, 99)
print('result: {}'.format(result))
在這個例子中,使用前面的總和99來初始化reduce()計算的值。
如果沒有initializer參數(shù),那么只有一個元素的序列會自動縮減為這個值。空列表會生成一個錯誤,除非提供一個initializer參數(shù)。
import functools
def do_reduce(a, b):
print('do_reduce({}, {})'.format(a, b))
return a + b
print('Single item in sequence:',
functools.reduce(do_reduce, [1]))
print('Single item in sequence with initializer:',
functools.reduce(do_reduce, [1], 99))
print('Empty sequence with initializer:',
functools.reduce(do_reduce, [], 99))
try:
print('Empty sequence:', functools.reduce(do_reduce, []))
except TypeError as err:
print('ERROR: {}'.format(err))
由于initializer參數(shù)相當(dāng)于一個默認值,但也要與新值結(jié)合(如果輸入序列不為空),所以必須仔細考慮這個參數(shù)的使用是否適當(dāng),這很重要。如果默認值與新值結(jié)合沒有意義,那么最好是捕獲TypeError而不是傳入一個initializer參數(shù)。
1.5 泛型函數(shù)
在類似Python的動態(tài)類型語言中,通常需要基于參數(shù)的類型完成稍有不同的操作,特別是在處理元素列表與單個元素的差別時。直接檢查參數(shù)的類型固然很簡單,但是有些情況下,行為差異可能被隔離到單個的函數(shù)中,對于這些情況,functools提供了singledispatch()修飾符來注冊一組泛型函數(shù)(generic function),可以根據(jù)函數(shù)第一個參數(shù)類型自動切換。
import functools
@functools.singledispatch
def myfunc(arg):
print('default myfunc({!r})'.format(arg))
@myfunc.register(int)
def myfunc_int(arg):
print('myfunc_int({})'.format(arg))
@myfunc.register(list)
def myfunc_list(arg):
print('myfunc_list()')
for item in arg:
print(' {}'.format(item))
myfunc('string argument')
myfunc(1)
myfunc(2.3)
myfunc(['a', 'b', 'c'])
新函數(shù)的register()屬性相當(dāng)于另一個修飾符,用于注冊替代實現(xiàn)。用singledispatch()包裝的第一個函數(shù)是默認實現(xiàn),在未指定其他類型特定函數(shù)時就使用這個默認實現(xiàn),在這個例子中特定類型就是float。
沒有找到這個類型的完全匹配時,會計算繼承順序,并使用最接近的匹配類型。
import functools
class A:
pass
class B(A):
pass
class C(A):
pass
class D(B):
pass
class E(C, D):
pass
@functools.singledispatch
def myfunc(arg):
print('default myfunc({})'.format(arg.__class__.__name__))
@myfunc.register(A)
def myfunc_A(arg):
print('myfunc_A({})'.format(arg.__class__.__name__))
@myfunc.register(B)
def myfunc_B(arg):
print('myfunc_B({})'.format(arg.__class__.__name__))
@myfunc.register(C)
def myfunc_C(arg):
print('myfunc_C({})'.format(arg.__class__.__name__))
myfunc(A())
myfunc(B())
myfunc(C())
myfunc(D())
myfunc(E())
在這個例子中,類D和E與已注冊的任何泛型函數(shù)都不完全匹配,所選擇的函數(shù)取決于類層次結(jié)構(gòu)。
總結(jié)
到此這篇關(guān)于Python3標(biāo)準(zhǔn)庫之functools管理函數(shù)的工具詳解的文章就介紹到這了,更多相關(guān)Python3 functools管理函數(shù)工具內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python functools模塊學(xué)習(xí)總結(jié)
- Python中functools模塊的常用函數(shù)解析
- Python中functools模塊函數(shù)解析
- Python使用functools實現(xiàn)注解同步方法
- Python編程functools模塊創(chuàng)建修改的高階函數(shù)解析
- Python的functools模塊使用及說明
- Python庫functools示例詳解
- Python中的functools partial詳解
- python高階函數(shù)functools模塊的具體使用
- Python中Functools模塊的高級操作詳解
- Python函數(shù)式編程模塊functools的使用與實踐
相關(guān)文章
Python騷操作完美實現(xiàn)短視頻偽原創(chuàng)
剪輯的視頻上傳到某平臺碰到降權(quán)怎么辦?視頻平臺都有一套自己的鑒別算法,專門用于處理視頻的二次剪輯,本篇我們來用python做一些特殊處理2022-02-02
python 獲取sqlite3數(shù)據(jù)庫的表名和表字段名的實例
今天小編就為大家分享一篇python 獲取sqlite3數(shù)據(jù)庫的表名和表字段名的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07
python 繪制擬合曲線并加指定點標(biāo)識的實現(xiàn)
這篇文章主要介紹了python 繪制擬合曲線并加指定點標(biāo)識的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07

