亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網(wǎng)為廣大站長提供免費收錄網(wǎng)站服務(wù),提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.430618.com 】, 免友鏈快審服務(wù)(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

本篇文章給大家?guī)砹岁P(guān)于Python的相關(guān)知識,主要介紹了Python的functools模塊使用及說明,具有很好的參考價值,下面一起來看一下,希望對大家有幫助。

Python的functools模塊使用總結(jié)

partial

用于創(chuàng)建一個偏函數(shù),將默認(rèn)參數(shù)包裝一個可調(diào)用對象,返回結(jié)果也是可調(diào)用對象。

偏函數(shù)可以固定住原函數(shù)的部分參數(shù),從而在調(diào)用時更簡單。

from functools import partial
 
int2 = partial(int, base=8)
print(int2('123'))
# 83

update_wrapper

使用 partial 包裝的函數(shù)是沒有__name__和__doc__屬性的。

update_wrapper 作用:將被包裝函數(shù)的__name__等屬性,拷貝到新的函數(shù)中去。

from functools import update_wrapper
def wrap2(func):
    def inner(*args):
        return func(*args)
    return update_wrapper(inner, func)
 
@wrap2
def demo():
    print('hello world')
 
print(demo.__name__)
# demo

wraps

warps 函數(shù)是為了在裝飾器拷貝被裝飾函數(shù)的__name__。

就是在update_wrapper上進行一個包裝

from functools import wraps
def wrap1(func):
    @wraps(func)    # 去掉就會返回inner
    def inner(*args):
        print(func.__name__)
        return func(*args)
    return inner
 
@wrap1
def demo():
    print('hello world')
 
print(demo.__name__)
# demo

reduce

在 Python2 中等同于內(nèi)建函數(shù) reduce

函數(shù)的作用是將一個序列歸納為一個輸出

reduce(function, sequence, startValue)
 
from functools import reduce
 
l = range(1,50)
print(reduce(lambda x,y:x+y, l))
# 1225

cmp_to_key

在 list.sort 和 內(nèi)建函數(shù) sorted 中都有一個 key 參數(shù)

x = ['hello','worl','ni']
x.sort(key=len)
print(x)
# ['ni', 'worl', 'hello']

Python3 之前還提供了cmp參數(shù)來比較兩個元素

cmp_to_key 函數(shù)就是用來將老式的比較函數(shù)轉(zhuǎn)化為 key 函數(shù)


lru_cache

允許我們將一個函數(shù)的返回值快速地緩存或取消緩存。

該裝飾器用于緩存函數(shù)的調(diào)用結(jié)果,對于需要多次調(diào)用的函數(shù),而且每次調(diào)用參數(shù)都相同,則可以用該裝飾器緩存調(diào)用結(jié)果,從而加快程序運行。

該裝飾器會將不同的調(diào)用結(jié)果緩存在內(nèi)存中,因此需要注意內(nèi)存占用問題。

from functools import lru_cache
@lru_cache(maxsize=30)  # maxsize參數(shù)告訴lru_cache緩存最近多少個返回值
def fib(n):
    if n < 2:
        return n
    return fib(n-1) + fib(n-2)
print([fib(n) for n in range(10)])
fib.cache_clear()   # 清空緩存

singledispatch

單分發(fā)器, Python3.4新增,用于實現(xiàn)泛型函數(shù)。

根據(jù)單一參數(shù)的類型來判斷調(diào)用哪個函數(shù)。

from functools import singledispatch
@singledispatch
def fun(text):
    print('String:' + text)
 
@fun.register(int)
def _(text):
    print(text)
 
@fun.register(list)
def _(text):
    for k, v in enumerate(text):
        print(k, v)
 
@fun.register(float)
@fun.register(tuple)
def _(text):
    print('float, tuple')
fun('i am is hubo')
fun(123)
fun(['a','b','c'])
fun(1.23)
print(fun.registry)    # 所有的泛型函數(shù)
print(fun.registry[int])    # 獲取int的泛型函數(shù)
# String:i am is hubo
# 123
# 0 a
# 1 b
# 2 c
# float, tuple
# {<class 'object'>: <function fun at 0x106d10f28>, <class 'int'>: <function _ at 0x106f0b9d8>, <class 'list'>: <function _ at 0x106f0ba60>, <class 'tuple'>: <function _ at 0x106f0bb70>, <class 'float'>: <function _ at 0x106f0bb70>}
# <function _ at 0x106f0b9d8>


分享到:
標(biāo)簽:Python functools模塊使用總結(jié)
用戶無頭像

網(wǎng)友整理

注冊時間:

網(wǎng)站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨大挑戰(zhàn)2018-06-03

數(shù)獨一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學(xué)四六

運動步數(shù)有氧達人2018-06-03

記錄運動步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績評定2018-06-03

通用課目體育訓(xùn)練成績評定