Python中functools模塊的常用函數(shù)解析
1.partial
首先是partial函數(shù),它可以重新綁定函數(shù)的可選參數(shù),生成一個callable的partial對象:
>>> int('10') # 實際上等同于int('10', base=10)和int('10', 10)
10
>>> int('10', 2) # 實際上是int('10', base=2)的縮寫
2
>>> from functools import partial
>>> int2 = partial(int, 2) # 這里我沒寫base,結果就出錯了
>>> int2('10')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: an integer is required
>>> int2 = partial(int, base=2) # 把base參數(shù)綁定在int2這個函數(shù)里
>>> int2('10') # 現(xiàn)在缺省參數(shù)base被設為2了
2
>>> int2('10', 3) # 沒加base,結果又出錯了
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: keyword parameter 'base' was given by position and by name
>>> int2('10', base=3)
3
>>> type(int2)
<type 'functools.partial'>
從中可以看出,唯一要注意的是可選參數(shù)必須寫出參數(shù)名。
2.update_wrapper
接著是update_wrapper函數(shù),它可以把被封裝函數(shù)的__name__、__module__、__doc__和 __dict__都復制到封裝函數(shù)去:
#-*- coding: gbk -*- def thisIsliving(fun): def living(*args, **kw): return fun(*args, **kw) + '活著就是吃嘛。' return living @thisIsliving def whatIsLiving(): "什么是活著" return '對啊,怎樣才算活著呢?' print whatIsLiving() print whatIsLiving.__doc__ print from functools import update_wrapper def thisIsliving(fun): def living(*args, **kw): return fun(*args, **kw) + '活著就是吃嘛。' return update_wrapper(living, fun) @thisIsliving def whatIsLiving(): "什么是活著" return '對啊,怎樣才算活著呢?' print whatIsLiving() print whatIsLiving.__doc__
結果:
對啊,怎樣才算活著呢?活著就是吃嘛。 None 對啊,怎樣才算活著呢?活著就是吃嘛。 什么是活著
不過也沒多大用處,畢竟只是少寫了4行賦值語句而已。
3.wraps
再有是wraps函數(shù),它將update_wrapper也封裝了進來:
#-*- coding: gbk -*- from functools import wraps def thisIsliving(fun): @wraps(fun) def living(*args, **kw): return fun(*args, **kw) + '活著就是吃嘛。' return living @thisIsliving def whatIsLiving(): "什么是活著" return '對啊,怎樣才算活著呢?' print whatIsLiving() print whatIsLiving.__doc__
結果還是一樣的:
對啊,怎樣才算活著呢?活著就是吃嘛。 什么是活著
4.total_ordering
最后至于total_ordering函數(shù)則給予類豐富的排序方法,使用裝飾器簡化了操作。如果使用必須在類里面定義一個__lt__(),__le__(), __gt__(), 或__ge__()。應該給類添加一個__eq__() 方法。
from functools import total_ordering
@total_ordering
class Student(object):
def __init__(self, name):
self.name = name
def __eq__(self, other):
return self.name.lower() == other.name.lower()
def __lt__(self, other):
return self.name.lower() < other.name.lower()
a = Student('dan')
b = Student('mink')
print a > b
print a
print sorted([b, a])
打印結果
False <__main__.Student object at 0x7f16ecb194d0> [<__main__.Student object at 0x7f16ecb194d0>, <__main__.Student object at 0x7f16ecb195d0>]
- Python functools模塊學習總結
- Python中functools模塊函數(shù)解析
- Python使用functools實現(xiàn)注解同步方法
- Python3標準庫之functools管理函數(shù)的工具詳解
- Python編程functools模塊創(chuàng)建修改的高階函數(shù)解析
- Python的functools模塊使用及說明
- Python庫functools示例詳解
- Python中的functools partial詳解
- python高階函數(shù)functools模塊的具體使用
- Python中Functools模塊的高級操作詳解
- Python函數(shù)式編程模塊functools的使用與實踐
相關文章
windows下python 3.9 Numpy scipy和matlabplot的安裝教程詳解
這篇文章主要介紹了windows下python 3.9 Numpy scipy和matlabplot的安裝教程詳解,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11
pycharm打包py項目為.exe可執(zhí)行文件的兩種方式
本文主要介紹了pycharm打包py項目為.exe可執(zhí)行文件的兩種方式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-01-01
python argparse模塊通過后臺傳遞參數(shù)實例
這篇文章主要介紹了python argparse模塊通過后臺傳遞參數(shù)實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04
python PyQt5對象類型的判定及對象刪除操作詳細解讀
PyQt5主要是用來判定一個對象的類型,或者說是否繼承自某個類,本文給大家介紹python PyQt5對象類型的判定,對象刪除操作詳細解讀,感興趣的朋友一起看看吧2024-07-07

