偏函數應用

Currying能泛化稱爲偏函數應用(Partial Function Application, PFA),這種函數將任意數量(順序)的參數的函數轉化成另外一個帶剩餘參數的函數對象。編程

from operator import add, mul
from functools import partial
add1 = partial(add, 1)      #add1(x) == add(1, x)  
mul10 = partial(mul, 10)    #mul10 == mul(10, x)
baseTwo = partial(int, base = 2)
baseTwo.__doc__ = 'Convert base 2 string to an int'
baseTwo('10010')

摘自《Python核心編程》函數

相關文章
相關標籤/搜索