麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

python的functools進階模塊

2019-11-14 09:23:37
字體:
來源:轉載
供稿:網友
Python-進階-functools模塊小結functools.partialfunctool.update_wrapperfunctool.wrapsfunctools.reducefunctools.cmp_to_keyfunctools.total_ordering

文檔 地址

functools.partial

作用:

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

用一些默認參數包裝一個可調用對象,返回結果是可調用對象,并且可以像原始對象一樣對待

凍結部分函數位置函數或關鍵字參數,簡化函數,更少更靈活的函數參數調用

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

聲明:

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

當調用 urlunquote(args, *kargs)

相當于 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

應用:

典型的,函數在執行時,要帶上所有必要的參數進行調用。

然后,有時參數可以在函數被調用之前提前獲知。

這種情況下,一個函數有一個或多個參數預先就能用上,以便函數能用更少的參數進行調用。

functool.update_wrapper

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

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

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

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

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

before callhello worldcall_itwrap func: call_itbefore callhello world2hello2test hello

functool.wraps

調用函數裝飾器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'

結果

before callhello world3hello3test hello 3

functools.reduce

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

等同于內置函數reduce()

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

functools.cmp_to_key

functools.cmp_to_key(func)

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

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

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

例如:

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__這些方法中的至少一個,使用該裝飾器,則會自動的把其他幾個比較函數也實現在該類中

@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__']
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: sese在线视频 | 精久久久 | 日韩激情一区 | 久久综合综合 | 精品中文字幕在线播放 | 在线观看中文字幕av | 久草手机视频在线观看 | 成人精品一区二区 | 国产亚洲精彩视频 | 黄色的视频在线观看 | 黄色成人小视频 | 一本免费视频 | 黄色一级片在线观看 | 国产99视频在线观看 | 一夜新娘第三季免费观看 | 一级尻逼视频 | 亚洲精品成人在线视频 | 日日草夜夜操 | 国产一级免费在线视频 | 亚洲一区二区三区视频 | 国产高潮失禁喷水爽到抽搐视频 | 亚洲精品无码不卡在线播放he | 久久国产精品久久久久久久久久 | 成人毛片免费视频 | 蜜桃视频最新网址 | 欧美视频一区二区三区四区 | 久草欧美 | 国产激情精品一区二区三区 | 欧美激情综合在线 | 九九热在线精品视频 | 欧美成人一区二区三区电影 | 91久久极品少妇韩国 | 中国免费一级毛片 | 在线观看免费毛片视频 | 97视频 | 欧美熟videos肥婆 | 男女生羞羞视频网站在线观看 | 蜜桃视频在线观看视频 | 最新在线中文字幕 | 国内精品伊人久久久久网站 | 国产精品成人免费一区久久羞羞 |