88_functools.py 475 B

12345678910111213141516171819202122232425
  1. # test reduce
  2. from functools import reduce, partial
  3. # test reduce
  4. assert reduce(lambda x, y: x + y, [1, 2, 3, 4, 5]) == 15
  5. assert reduce(lambda x, y: x * y, [1, 2, 3, 4, 5]) == 120
  6. assert reduce(lambda x, y: x * y, [1, 2, 3, 4, 5], 10) == 1200
  7. # test partial
  8. def add(a, b):
  9. return a + b
  10. add_1 = partial(add, 1)
  11. assert add_1(2) == 3
  12. assert add_1(3) == 4
  13. def sub(a, b=1):
  14. return a - b
  15. sub_10 = partial(sub, b=10)
  16. assert sub_10(20) == 10
  17. assert sub_10(30) == 20