1. 程式人生 > >Python3(進階)-偏函式functools.prtial()

Python3(進階)-偏函式functools.prtial()

functools.partial(FuncA,p1,p2,…)用於把函式FuncA的前幾個引數分別固定為p1,p2,…

from functools import partial


def add(a, b, c):
    print("a=", a)
    print("b=", b)
    print("c=", c)
    return a + b + c


addPlus = partial(add, 1, 2)

print(addPlus(3))

輸出:

a= 1
b= 2
c= 3
6