10. 函数式编程模块¶
10.1. itertools¶
该模块标准化了一套核心的快速、内存有效的工具,它们本身或组合使用都是有用的。
In [1]: import itertools
In [3]: import operator
# 累加的
In [5]: list(itertools.accumulate([1,2,3,4,5],operator.add))
Out[5]: [1, 3, 6, 10, 15]
# 累乘的
In [6]: list(itertools.accumulate([1,2,3,4,5],operator.mul))
Out[6]: [1, 2, 6, 24, 120]
10.2. functools¶
functools模块是高阶函数:函数法或返回等功能。一般来说,为了这个模块的目的, 任何可调用对象都可以被当作一个函数处理。
10.3. operator¶
定义写操作方法
In [1]: import operator
In [2]: operator.abs
In [3]: operator.
abs() eq() ifloordiv() invert() itruediv() mod() rshift()
add() floordiv() ilshift() ior() ixor() mul() setitem()
and_() ge() imatmul() ipow() le() ne() sub()
attrgetter getitem() imod() irshift() length_hint() neg() truediv()
concat() gt() imul() is_() lshift() not_() truth()
contains() iadd() index() is_not() lt() or_() xor()
countOf() iand() indexOf() isub() matmul() pos()
delitem() iconcat() inv() itemgetter methodcaller pow()