码迷,mamicode.com
首页 > 其他好文 > 详细

偏函数+高阶函数

时间:2018-09-12 17:00:30      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:nbsp   UNC   return   ted   new   一个   手动   name   函数的参数   

-----------------------------------------------------------------------------偏函数----------------------------------------------------------------------

偏函数:函数的参数较多,且大多数固定,把这些参数默认的函数称为偏函数------创新函数

 

1.手动

def test(a,b,c,d):
    print(a + b+ c+d)
def test2(a,b ,c,d=1):
    print(a + b + c + d)
test2(1,2,3)

2.调用functools

import functools

def test(a,b,c,d):
    print(a +b +c +d)

test2 = functools.partial(test,c=1)

test2(1,2,3)

场景:

Int函数----------------int(字符串,base=2)

int:将字符串----→数字

手动----------------

newstr = "10010"
result = int(newstr, base = 2)
print(result)

调用

import functools

newstr = "100010"
int2 = functools.partail(int,base=2)
result = int2(newstr)
print(result)

---------------------------------------------------------------------------------高阶函数------------------------------------------------------------------------------------------------------

高阶函数;接收的参数中有另外一个函数

用处:方便构造要先干嘛的函数,要先做的可以构建一个小函数并当作参数传入主函数

例:sorted()函数

l = [{"name":"sz","age":18},{"name":"xw","age":19}

def getkey(x):
    return(x["age"])

result = sorted(l.key= getkey)

场景

def calculate(num1,num2,calculate2):
    print(calculate2(num1,num2)
def sum(a,b):
    return a+b
def jian(a,b):
    return a-b

calculate(2,3,sum)
calculate(2,3,jian)

 

偏函数+高阶函数

标签:nbsp   UNC   return   ted   new   一个   手动   name   函数的参数   

原文地址:https://www.cnblogs.com/dushuhubian/p/9635399.html

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