Python partial 工具函数

python

partial函数可以提高代码利用率和简洁性:

from functools import partial

def power(base, exponent, bias):

return base * exponent + bias

squared = partial(power, exponent=2, bias=100)

In [7]: power(2,2,100)                                                          

Out[7]: 104

In [8]: squared(2)                                                              

Out[8]: 104

In [9]: squared(2, bias=1000)                                                   

Out[9]: 1004

partial函数可以提高代码利用率和简洁性:

from functools import partial

def power(base, exponent, bias):

return base * exponent + bias

squared = partial(power, exponent=2, bias=100)

In [7]: power(2,2,100)                                                          

Out[7]: 104

In [8]: squared(2)                                                              

Out[8]: 104

In [9]: squared(2, bias=1000)                                                   

Out[9]: 1004

以上是 Python partial 工具函数 的全部内容, 来源链接: utcz.com/z/388645.html

回到顶部