首页 > 编程语言 > 详细

python 高阶函数: Partial(偏函数)

时间:2018-07-18 11:44:30      阅读:154      评论:0      收藏:0      [点我收藏+]
格式:

        functools.partail(函数,函数的参数) -------> int(x, base) ----->  functools.partial(int, base)


一、int函数

官网介绍:class int(x, base=10)  #x为字符串数字, 默认该字符串数字是十进制数, 返回值是十进制数



#!/usr/bin/python

##普通使用
print "int('10001', 2):", int('10001', 2)  #字符串数字‘10001’是2进制数, 返回值是十进制数

技术分享图片


##自定义使用

def int2(x, base=2):
    return int(x, base)
   
print "int2('1010101'):", int2('1010101')

技术分享图片


二、partial函数

官网介绍:

  • functools.partial(func[,*args][, **keywords])

  • Return a new partial object which when called will behave like funccalled with the positional arguments args and keyword arguments keywords. If more arguments are supplied to the call, they are appended to args. If additional keyword arguments are supplied, they extend and override keywords.


import functools  #导入模块

int3 = functools.partial(int, base=2)  # int3: 将int函数的base参数设置为默认值2进制
print "int3('100'):", int3('100')

技术分享图片


int4 = functools.partial(int, ‘2’)  #int4:将int函数的x参数设置为默认值‘2’ ,其base默认为十进制

print "int4('2'):", int4()

技术分享图片

python 高阶函数: Partial(偏函数)

原文:http://blog.51cto.com/13502993/2146244

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!