Python中functools模塊的常用函數(shù)解析
1.partial
首先是partial函數(shù),它可以重新綁定函數(shù)的可選參數(shù),生成一個(gè)callable的partial對(duì)象:
>>> int('10') # 實(shí)際上等同于int('10', base=10)和int('10', 10)
10
>>> int('10', 2) # 實(shí)際上是int('10', base=2)的縮寫
2
>>> from functools import partial
>>> int2 = partial(int, 2) # 這里我沒(méi)寫base,結(jié)果就出錯(cuò)了
>>> 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這個(gè)函數(shù)里
>>> int2('10') # 現(xiàn)在缺省參數(shù)base被設(shè)為2了
2
>>> int2('10', 3) # 沒(méi)加base,結(jié)果又出錯(cuò)了
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__都復(fù)制到封裝函數(shù)去:
#-*- coding: gbk -*- def thisIsliving(fun): def living(*args, **kw): return fun(*args, **kw) + '活著就是吃嘛。' return living @thisIsliving def whatIsLiving(): "什么是活著" return '對(duì)啊,怎樣才算活著呢?' 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 '對(duì)啊,怎樣才算活著呢?' print whatIsLiving() print whatIsLiving.__doc__
結(jié)果:
對(duì)啊,怎樣才算活著呢?活著就是吃嘛。 None 對(duì)啊,怎樣才算活著呢?活著就是吃嘛。 什么是活著
不過(guò)也沒(méi)多大用處,畢竟只是少寫了4行賦值語(yǔ)句而已。
3.wraps
再有是wraps函數(shù),它將update_wrapper也封裝了進(jìn)來(lái):
#-*- 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 '對(duì)啊,怎樣才算活著呢?' print whatIsLiving() print whatIsLiving.__doc__
結(jié)果還是一樣的:
對(duì)啊,怎樣才算活著呢?活著就是吃嘛。 什么是活著
4.total_ordering
最后至于total_ordering函數(shù)則給予類豐富的排序方法,使用裝飾器簡(jiǎn)化了操作。如果使用必須在類里面定義一個(gè)__lt__(),__le__(), __gt__(), 或__ge__()。應(yīng)該給類添加一個(gè)__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])
打印結(jié)果
False <__main__.Student object at 0x7f16ecb194d0> [<__main__.Student object at 0x7f16ecb194d0>, <__main__.Student object at 0x7f16ecb195d0>]
- Python functools模塊學(xué)習(xí)總結(jié)
- Python中functools模塊函數(shù)解析
- Python使用functools實(shí)現(xiàn)注解同步方法
- Python3標(biāo)準(zhǔn)庫(kù)之functools管理函數(shù)的工具詳解
- Python編程functools模塊創(chuàng)建修改的高階函數(shù)解析
- Python的functools模塊使用及說(shuō)明
- Python庫(kù)functools示例詳解
- Python中的functools partial詳解
- python高階函數(shù)functools模塊的具體使用
- Python中Functools模塊的高級(jí)操作詳解
- Python函數(shù)式編程模塊functools的使用與實(shí)踐
相關(guān)文章
TensorFlow深度學(xué)習(xí)之卷積神經(jīng)網(wǎng)絡(luò)CNN
這篇文章主要介紹了TensorFlow深度學(xué)習(xí)之卷積神經(jīng)網(wǎng)絡(luò)CNN2018-03-03
Django form表單與請(qǐng)求的生命周期步驟詳解
這篇文章主要介紹了Django-form表單與請(qǐng)求的生命周期,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
Java byte數(shù)組操縱方式代碼實(shí)例解析
這篇文章主要介紹了Java byte數(shù)組操縱方式代碼實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
Python實(shí)現(xiàn)文本特征提取的方法詳解
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)提取四種不同文本特征的方法,有字典文本特征提取、英文文本特征提取、中文文本特征提取和TF-IDF 文本特征提取,感興趣的可以了解一下2022-08-08
python3實(shí)現(xiàn)磁盤空間監(jiān)控
這篇文章主要為大家詳細(xì)介紹了python3實(shí)現(xiàn)磁盤空間監(jiān)控,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
Python numpy.array()生成相同元素?cái)?shù)組的示例
今天小編就為大家分享一篇Python numpy.array()生成相同元素?cái)?shù)組的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-11-11
Python爬蟲實(shí)例——scrapy框架爬取拉勾網(wǎng)招聘信息
這篇文章主要介紹了Python爬蟲實(shí)例——scrapy框架爬取拉勾網(wǎng)招聘信息的相關(guān)資料,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07
python 實(shí)現(xiàn)非極大值抑制算法(Non-maximum suppression, NMS)
這篇文章主要介紹了python 如何實(shí)現(xiàn)非極大值抑制算法(Non-maximum suppression, NMS),幫助大家更好的進(jìn)行機(jī)器學(xué)習(xí),感興趣的朋友可以了解下2020-10-10

