Python的functools模塊使用及說明_第1頁
Python的functools模塊使用及說明_第2頁
Python的functools模塊使用及說明_第3頁
Python的functools模塊使用及說明_第4頁
Python的functools模塊使用及說明_第5頁
全文預覽已結束

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

第Python的functools模塊使用及說明目錄partialupdate_wrapperwrapsreducecmp_to_keylru_cachesingledispatch

partial

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

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

fromfunctoolsimportpartial

int2=partial(int,base=8)

print(int2('123'))

#83

update_wrapper

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

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

fromfunctoolsimportupdate_wrapper

defwrap2(func):

definner(*args):

returnfunc(*args)

returnupdate_wrapper(inner,func)

@wrap2

defdemo():

print('helloworld')

print(demo.__name__)

#demo

wraps

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

就是在update_wrapper上進行一個包裝

fromfunctoolsimportwraps

defwrap1(func):

@wraps(func)

#去掉就會返回inner

definner(*args):

print(func.__name__)

returnfunc(*args)

returninner

@wrap1

defdemo():

print('helloworld')

print(demo.__name__)

#demo

reduce

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

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

reduce(function,sequence,startValue)

fromfunctoolsimportreduce

l=range(1,50)

print(reduce(lambdax,y:x+y,l))

#1225

cmp_to_key

在list.sort和內建函數(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ù)轉化為key函數(shù)

lru_cache

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

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

該裝飾器會將不同的調用結果緩存在內存中,因此需要注意內存占用問題。

fromfunctoolsimportlru_cache

@lru_cache(maxsize=30)

#maxsize參數(shù)告訴lru_cache緩存最近多少個返回值

deffib(n):

ifn2:

returnn

returnfib(n-1)+fib(n-2)

print([fib(n)forninrange(10)])

fib.cache_clear()

#清空緩存

singledispatch

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

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

fromfunctoolsimportsingledispatch

@singledispatch

deffun(text):

print('String:'+text)

@fun.register(int)

def_(text):

print(text)

@fun.register(list)

def_(text):

fork,vinenumerate(text):

print(k,v)

@fun.register(float)

@fun.register(tuple)

def_(text):

print('float,tuple')

fun('iamishubo')

fun(123)

fun(['a','b','c'])

fun(1.23)

print(fun.registry)

#所有的泛型函數(shù)

print(fun.registry[int])

#獲取int的泛型函數(shù)

#String:iamishubo

#123

#0a

#1b

#2c

#float,tuple

#{class'object':functionfunat0x106d10f28,class'int':function_at0x106f0b9d8,class'list':function_at0x106f0ba60,class'tuple':funct

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論