偏函数+高阶函数

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

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

 

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)

 

转载于:https://www.cnblogs.com/dushuhubian/p/9635399.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值