欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

Python functools模塊學(xué)習(xí)總結(jié)

系統(tǒng) 1610 0

文檔 地址

functools.partial

作用:

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

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

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

復(fù)制代碼 代碼如下:

#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

聲明:
復(fù)制代碼 代碼如下:

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

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

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

E.g:

復(fù)制代碼 代碼如下:

import functools

def add(a, b):
??? return a + b

add(4, 2)
6

plus3 = functools.partial(add, 3)
plus5 = functools.partial(add, 5)

plus3(4)
7
plus3(7)
10

plus5(10)
15

應(yīng)用:

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

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

這種情況下,一個函數(shù)有一個或多個參數(shù)預(yù)先就能用上,以便函數(shù)能用更少的參數(shù)進(jìn)行調(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)

復(fù)制代碼 代碼如下:

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

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

復(fù)制代碼 代碼如下:

#!/usr/bin/env python
# encoding: utf-8

def wrap(func):
??? def call_it(*args, **kwargs):
??????? """wrap func: call_it"""
??????? print 'before call'
??????? return func(*args, **kwargs)
??? return call_it

@wrap
def hello():
??? """say hello"""
??? print 'hello world'

from functools import update_wrapper
def wrap2(func):
??? def call_it(*args, **kwargs):
??????? """wrap func: call_it2"""
??????? print 'before call'
??????? return func(*args, **kwargs)
??? return update_wrapper(call_it, func)

@wrap2
def hello2():
??? """test hello"""
??? print 'hello world2'

if __name__ == '__main__':
??? hello()
??? print hello.__name__
??? print hello.__doc__

??? print
??? hello2()
??? print hello2.__name__
??? print hello2.__doc__

得到結(jié)果:

復(fù)制代碼 代碼如下:

before call
hello world
call_it
wrap func: call_it

before call
hello world2
hello2
test hello

functool.wraps

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

復(fù)制代碼 代碼如下:

from functools import wraps
def wrap3(func):
??? @wraps(func)
??? def call_it(*args, **kwargs):
??????? """wrap func: call_it2"""
??????? print 'before call'
??????? return func(*args, **kwargs)
??? return call_it

@wrap3
def hello3():
??? """test hello 3"""
??? print 'hello world3'


結(jié)果
復(fù)制代碼 代碼如下:

before call
hello world3
hello3
test hello 3

functools.reduce

復(fù)制代碼 代碼如下:

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ù)在期望序列中的位置

例如:

復(fù)制代碼 代碼如下:

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

functools.total_ordering

復(fù)制代碼 代碼如下:

functools.total_ordering(cls)

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

@total_ordering
class 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)

得到
復(fù)制代碼 代碼如下:

['__doc__', '__eq__', '__ge__', '__gt__', '__le__', '__lt__', '__module__']


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 久久综合婷婷香五月 | 精品国产欧美一区二区 | 成人涩涩屋福利视频 | 欧美成人看片黄a免费看 | 四虎网址| 热99久久| 我和我的祖国电影在线观看免费版高清 | 亚洲精品国精品久久99热 | av一区在线观看 | 色狠狠色狠狠综合天天 | 亚洲av毛片久久久久 | 欧美激情黄色 | 国产伦精品一区二区三区精品视频 | 91免费视频版 | 久久噜噜噜精品国产亚洲综合 | 欧美激情网 | av在线国产精品 | 欧美一级电影网 | 日韩中文字幕免费在线观看 | 久久精品成人免费国产片桃视频 | 日韩黄色精品视频 | 亚洲国产乱 | 亚洲精品久久久久一区二区三区 | 男人用嘴添女人下身免费视频 | 亚洲综合成人网 | 26uuu天天夜夜综合 | 亚洲国产精品热久久2022 | 国产精品久久久久久久久免费相片 | 又黄又爽的成人免费网站 | 亚洲精品久中文字幕 | 又大又粗进出白浆直流动态图 | 国产精品国产亚洲精品不卡 | 久久精品呦女 | 日韩在线观看一区二区不卡视频 | 久久精品久久精品国产大片 | 成人激情四射 | 国产成人一区二区三区久久久 | 久久久久国产一区二区三区 | 奇米影视88888 | 亚洲天堂中文字幕 | 久久精品无码一区二区日韩av |