functools模块

1 particial :对局部函数进行控制,这个局部最常见的是对参数的控制

使用规则:

  a : 将前面连续的参数固定,就可以直接继续按照原来的参数继续调用。如

# def test(a, b, c, d):
#     print(a, b, c, d)
# test1 = partial(test, 1, 2)
# test1(3,4)

  b : 将后面的连续参数固定,就可以直接继续使用原来的参数进行调用。如

# from functools import partial
# def test(a,  b, c, d):
#     print(a, b, c, d)
# test4 = partial(test, c=3, d=4)
# test4(1,2)

  c : 如果默认参数值不是连续的或者是直接对前面的连续参数赋值,那么就需要使用关键字参数进行调用,如

# from functools import partial
# def test(a, b, c, d):
#     print(a, b, c, d)
# test4 = partial(test,b=3, d=4)
# test4(1,c=2)

2 reduce函数

from functools import reduce
sum = reduce(lambda x, y: x + y, (1, 2, 3, 4, 5, 6, 7))
print(sum)

  

原文地址:https://www.cnblogs.com/gyh412724/p/10479221.html