python 高阶函数: Partial(偏函数)
格式:
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. Ifmore arguments are supplied to the call, they are appended to args. Ifadditional 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()
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。