zoukankan      html  css  js  c++  java
  • 偏函数+高阶函数

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

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

    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)
  • 相关阅读:
    快速搭建http server
    cwmp part2 测试基础RPC
    Leetcode-5223 Queens That Can Attack the King(可以攻击国王的皇后)
    Leetcode-5222 Split a String in Balanced Strings(分割平衡字符串)
    Leetcode-5224 Dice Roll Simulation(掷骰子模拟)
    P2604-[ZJOI2010]网络扩容
    P2053-[SCOI2007]修车
    P2153-[SDOI2009]晨跑
    P2774 方格取数问题
    P2763-试题库问题
  • 原文地址:https://www.cnblogs.com/dushuhubian/p/9635399.html
Copyright © 2011-2022 走看看