国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

python的functools進階模塊

2019-11-14 09:27:42
字體:
供稿:網(wǎng)友
Python-進階-functools模塊小結(jié)functools.partialfunctool.update_wrapperfunctool.wrapsfunctools.reducefunctools.cmp_to_keyfunctools.total_ordering

文檔 地址

functools.partial

作用:

functools.partial 通過包裝手法,允許我們 "重新定義" 函數(shù)簽名

用一些默認(rèn)參數(shù)包裝一個可調(diào)用對象,返回結(jié)果是可調(diào)用對象,并且可以像原始對象一樣對待

凍結(jié)部分函數(shù)位置函數(shù)或關(guān)鍵字參數(shù),簡化函數(shù),更少更靈活的函數(shù)參數(shù)調(diào)用

#args/keyWords 調(diào)用partial時參數(shù)def partial(func, *args, **keywords):    def newfunc(*fargs, **fkeywords):        newkeywords = keywords.copy()        newkeywords.update(fkeywords)        return func(*(args + fargs), **newkeywords) #合并,調(diào)用原始函數(shù),此時用了partial的參數(shù)    newfunc.func = func    newfunc.args = args    newfunc.keywords = keywords    return newfunc

聲明:

urlunquote = functools.partial(urlunquote, encoding='latin1')

當(dāng)調(diào)用 urlunquote(args, *kargs)

相當(dāng)于 urlunquote(args, *kargs, encoding='latin1')

E.g:

import functoolsdef add(a, b):    return a + badd(4, 2)6plus3 = functools.partial(add, 3)plus5 = functools.partial(add, 5)plus3(4)7plus3(7)10plus5(10)15

應(yīng)用:

典型的,函數(shù)在執(zhí)行時,要帶上所有必要的參數(shù)進行調(diào)用。

然后,有時參數(shù)可以在函數(shù)被調(diào)用之前提前獲知。

這種情況下,一個函數(shù)有一個或多個參數(shù)預(yù)先就能用上,以便函數(shù)能用更少的參數(shù)進行調(diào)用。

functool.update_wrapper

默認(rèn)partial對象沒有__name__和__doc__, 這種情況下,對于裝飾器函數(shù)非常難以debug.使用update_wrapper(),從原始對象拷貝或加入現(xiàn)有partial對象

它可以把被封裝函數(shù)的__name__、module、__doc__和 __dict__都復(fù)制到封裝函數(shù)去(模塊級別常量WRAPPER_ASSIGNMENTS, WRAPPER_UPDATES)

>>> functools.WRAPPER_ASSIGNMENTS('__module__', '__name__', '__doc__')>>> functools.WRAPPER_UPDATES('__dict__',)

這個函數(shù)主要用在裝飾器函數(shù)中,裝飾器返回函數(shù)反射得到的是包裝函數(shù)的函數(shù)定義而不是原始函數(shù)定義

#!/usr/bin/env python# encoding: utf-8def wrap(func):    def call_it(*args, **kwargs):        """wrap func: call_it"""        得到結(jié)果:

before callhello worldcall_itwrap func: call_itbefore callhello world2hello2test hello

functool.wraps

調(diào)用函數(shù)裝飾器partial(update_wrapper, wrapped=wrapped, assigned=assigned, updated=updated)的簡寫

from functools import wrapsdef wrap3(func):    @wraps(func)    def call_it(*args, **kwargs):        """wrap func: call_it2"""        print 'before call'        return func(*args, **kwargs)    return call_it@wrap3def hello3():    """test hello 3"""    print 'hello world3'

結(jié)果

before callhello world3hello3test hello 3

functools.reduce

functools.reduce(function, iterable[, initializer])

等同于內(nèi)置函數(shù)reduce()

用這個的原因是使代碼更兼容(python3)

functools.cmp_to_key

functools.cmp_to_key(func)

將老式鼻尖函數(shù)轉(zhuǎn)換成key函數(shù),用在接受key函數(shù)的方法中(such as sorted(), min(), max(), heapq.nlargest(), heapq.nsmallest(), itertools.groupby())

一個比較函數(shù),接收兩個參數(shù),小于,返回負(fù)數(shù),等于,返回0,大于返回整數(shù)

key函數(shù),接收一個參數(shù),返回一個表明該參數(shù)在期望序列中的位置

例如:

sorted(iterable, key=cmp_to_key(locale.strcoll))  # locale-aware sort order

functools.total_ordering

functools.total_ordering(cls)

這個裝飾器是在python2.7的時候加上的,它是針對某個類如果定義了__lt__、le、gt、__ge__這些方法中的至少一個,使用該裝飾器,則會自動的把其他幾個比較函數(shù)也實現(xiàn)在該類中

@total_orderingclass Student:    def __eq__(self, other):        return ((self.lastname.lower(), self.firstname.lower()) ==                (other.lastname.lower(), other.firstname.lower()))    def __lt__(self, other):        return ((self.lastname.lower(), self.firstname.lower()) <                (other.lastname.lower(), other.firstname.lower()))print dir(Student)

得到

['__doc__', '__eq__', '__ge__', '__gt__', '__le__', '__lt__', '__module__']
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 莆田市| 甘孜| 马鞍山市| 四川省| 得荣县| 泰州市| 瑞丽市| 图木舒克市| 德阳市| 浑源县| 息烽县| 泸水县| 时尚| 蒙山县| 汝州市| 临高县| 贞丰县| 兴文县| 阳朔县| 平和县| 土默特左旗| 三穗县| 林口县| 个旧市| 丰镇市| 日土县| 会宁县| 湘潭县| 林西县| 水富县| 马山县| 夏津县| 莱西市| 资源县| 阿鲁科尔沁旗| 巴塘县| 岢岚县| 永清县| 得荣县| 合作市| 峨边|