码迷,mamicode.com
首页 > 编程语言 > 详细

python偏函数

时间:2017-08-08 21:39:22      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:too   cto   模块   ret   结果   默认   tool   python   pre   

python中functools.partial模块,可以用来定义偏函数,如:

def func(x, y):
    return x+y
result = func(2,10)
print(result)

运行结果:

12

可以将带默认值的函数,使用functools.partial进行封装,封装后的函数叫做偏函数

def func(x, y, z):
    return x-y-z
func2 = functools.partial(func, 5)
result = func2(2,1)
print(result)

运行结果:

2

函数的默认参数,放在所有参数的左边。

python偏函数

标签:too   cto   模块   ret   结果   默认   tool   python   pre   

原文地址:http://www.cnblogs.com/shijingjing07/p/7308805.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!