温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-06-01 06:09:15 来源:网络 阅读:1482 作者:虎皮喵的喵 栏目:编程语言

格式:

        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进制数, 返回值是十进制数

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


##自定义使用

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

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


二、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')

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


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

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

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI