Python-進階-functools模塊小結

Python-進階-functools模塊小結

文檔 地址html

functools.partial

做用:python

functools.partial 經過包裝手法,容許咱們 "從新定義" 函數簽名app

用一些默認參數包裝一個可調用對象,返回結果是可調用對象,而且能夠像原始對象同樣對待python2.7

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

#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

聲明:post

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

當調用 urlunquote(args, *kargs)url

至關於 urlunquote(args, *kargs, encoding='latin1')spa

E.g:debug

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

應用:htm

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

而後,有時參數能夠在函數被調用以前提早獲知。

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

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-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

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

等同於內置函數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__、legt、__ge__這些方法中的至少一個,使用該裝飾器,則會自動的把其餘幾個比較函數也實如今該類中

@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__']
相關文章
相關標籤/搜索