最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python functools模塊學習總結

 更新時間:2015年05月09日 12:02:36   投稿:junjie  
這篇文章主要介紹了Python functools模塊學習總結,本文講解了functools.partial、functool.update_wrapper、functool.wraps、functools.reduce、functools.cmp_to_key、functools.total_ordering等方法的使用實例,需要的朋友可以參考下

文檔 地址

functools.partial

作用:

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

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

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

復制代碼 代碼如下:

#args/keywords 調用partial時參數(shù)
def partial(func, *args, **keywords):
    def newfunc(*fargs, **fkeywords):
        newkeywords = keywords.copy()
        newkeywords.update(fkeywords)
        return func(*(args + fargs), **newkeywords) #合并,調用原始函數(shù),此時用了partial的參數(shù)
    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 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

應用:

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

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

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

functool.update_wrapper

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

它可以把被封裝函數(shù)的__name__、module、__doc__和 __dict__都復制到封裝函數(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-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__

得到結果:

復制代碼 代碼如下:

before call
hello world
call_it
wrap func: call_it

before call
hello world2
hello2
test hello

functool.wraps

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

復制代碼 代碼如下:

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'


結果
復制代碼 代碼如下:

before call
hello world3
hello3
test hello 3

functools.reduce

復制代碼 代碼如下:

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

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

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

functools.cmp_to_key

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

一個比較函數(shù),接收兩個參數(shù),小于,返回負數(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_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)

得到
復制代碼 代碼如下:

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

相關文章

  • Python 數(shù)據(jù)科學 Matplotlib圖庫詳解

    Python 數(shù)據(jù)科學 Matplotlib圖庫詳解

    Matplotlib 是 Python 的二維繪圖庫,用于生成符合出版質量或跨平臺交互環(huán)境的各類圖形。今天通過本文給大家分享Python 數(shù)據(jù)科學 Matplotlib的相關知識,感興趣的朋友一起看看吧
    2021-07-07
  • 六個實用Pandas數(shù)據(jù)處理代碼

    六個實用Pandas數(shù)據(jù)處理代碼

    這篇文章主要介紹了六個實用Pandas數(shù)據(jù)處理代碼,文章圍繞主題相相關內容,具有一定的參考價價值,需要的小伙伴可以參考一下
    2022-05-05
  • python中的reduce內建函數(shù)使用方法指南

    python中的reduce內建函數(shù)使用方法指南

    python中的reduce內建函數(shù)是一個二元操作函數(shù),他用來將一個數(shù)據(jù)集合(鏈表,元組等)中的所有數(shù)據(jù)進行下列操作:用傳給reduce中的函數(shù) func()(必須是一個二元操作函數(shù))先對集合中的第1,2個數(shù)據(jù)進行操作,得到的結果再與第三個數(shù)據(jù)用func()函數(shù)運算,最后得到一個結果
    2014-08-08
  • Python調用API接口實現(xiàn)人臉識別

    Python調用API接口實現(xiàn)人臉識別

    本文主要介紹了Python調用API接口實現(xiàn)人臉識別,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-02-02
  • 15個Python運行速度優(yōu)化技巧分享

    15個Python運行速度優(yōu)化技巧分享

    這篇文章主要為大家詳細介紹了15個Python運行速度優(yōu)化技巧,文中的示例代碼講解詳細,具有一定的借鑒價值,有需要的小伙伴可以參考一下
    2025-02-02
  • pip安裝包成功后無法import的解決過程

    pip安裝包成功后無法import的解決過程

    作者列舉了幾個Python環(huán)境管理中遇到的問題,包括使用sudo安裝導致地址問題、Conda進入錯誤環(huán)境、`python -m pip install XXX`命令指定安裝環(huán)境等,這些問題可能由于環(huán)境配置或命令使用不當導致
    2026-05-05
  • Python初識二叉樹續(xù)之實戰(zhàn)binarytree

    Python初識二叉樹續(xù)之實戰(zhàn)binarytree

    binarytree庫是一個Python的第三方庫,這個庫實現(xiàn)了一些二叉樹相關的常用方法,使用二叉樹時,可以直接調用,不需要再自己實現(xiàn),下面這篇文章主要給大家介紹了關于Python初識二叉樹之實戰(zhàn)binarytree的相關資料,需要的朋友可以參考下
    2022-05-05
  • 詳解如何使用Python在PDF文檔中創(chuàng)建動作

    詳解如何使用Python在PDF文檔中創(chuàng)建動作

    PDF格式因其跨平臺兼容性和豐富的功能集而成為許多行業(yè)中的首選文件格式,其中,PDF中的動作(Action) 功能尤為突出,本文將介紹如何使用Python在PDF文檔中創(chuàng)建動作,需要的朋友可以參考下
    2024-09-09
  • python-Web-flask-視圖內容和模板知識點西寧街

    python-Web-flask-視圖內容和模板知識點西寧街

    在本篇文章里小編給大家分享了關于python-Web-flask-視圖內容和模板的相關知識點內容,有需要的朋友們參考學習下。
    2019-08-08
  • Python如何使用struct.unpack處理二進制文件

    Python如何使用struct.unpack處理二進制文件

    這篇文章主要介紹了Python如何使用struct.unpack處理二進制文件問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02

最新評論

吉林市| 玛纳斯县| 长汀县| 桂阳县| 定结县| 天峻县| 澎湖县| 呼图壁县| 武隆县| 麻江县| 昌都县| 米林县| 三穗县| 安仁县| 汉寿县| 庆元县| 白山市| 海兴县| 山阳县| 尼勒克县| 汉阴县| 信阳市| 双鸭山市| 盘锦市| 盐亭县| 安义县| 吉木乃县| 双柏县| 清水县| 磐安县| 昌江| 卫辉市| 中西区| 长乐市| 晋中市| 大悟县| 乌鲁木齐县| 常熟市| 方城县| 安多县| 宽甸|