functoolsfunctools 包含了用于创建装饰函数,启动面向切面的编程,超出面向对象编程范围的代码复用,同时提供了装饰函数用于丰富的快捷比较的API, partial 模块还创建了包含函数参数的函数引用,也就是偏函数partial 偏函数partial 的作用在于如果存在一个函数的参数过多,可以通过partial 固定某一些参数,需要的时候使用关键字参数传入即可.通过一个简单的例子理解import functoolsdef myfunc(a,b): print(“This is myfuc params:{},{}".format(a,b))a = functools.partial(myfunc,b=1)a(10000)可以看到,本来调用myfunc的话,要传入两个参数,现在通过固定住某些参数,可以直接调用一个参数即可。除此之外,还可以通过另外一种方式来进行传值import functoolsdef myfunc(a,b): print(“This is myfuc params:{},{}".format(a,b))a = functools.partial(myfunc,b=1)value= {“a”:1000}a(**value)Comparisonfunctools还提供了丰富用于比较的API,在python2 中,在一个类中可以定义 cmp() 方法,用于对象中的比较操作,python3 废除了这样的做法,因为提供了更加详细的API方法,比如 lt() , le(), eq(),ne(),gt(),ge() 这些方法的含义如下:lt:less than 小于le:less than or equal to 小于等于eq:equal to 等于ne:not equal to 不等于ge:greater than or equal to 大于等于gt:greater than 大于functools 提供了一个装饰器,让我们不需要写这么多定义,只要写一个,其他定义也会加上去。 看一个简单的例子import functools@functools.total_orderingclass MyObject(): def init(self,priority): self.priority = priority def eq(self,other): print(‘dengyu’) return self.priority == other.priority def lt(self,other): return self.priority < other.priorityif name ==”main”: a = MyObject(1) print(dir(a))在实际实验中,加不加并没有区别。仅作了解lru_cache这是个有趣的装饰器,传入的参数被打上了hash,当下一次传入的参数是一样的时候,就会从cache中直接取出对应的值,而不需要进行重新的运算。一个简单的例子import functools@functools.lru_cache()def test_method(a,b): print(“execute {} * {} = {}".format(a,b,ab)) return abs = 0for i in range(2): for j in range(2): s+=test_method(i,j)print(test_method.cache_info())for i in range(2): for j in range(3): s+=test_method(i,j)print(test_method.cache_info())print(s) # 4 说明该执行的还是有执行,只不过是从cache中直接取出而已通用函数对于python来说,很难去固定一个参数必须是什么类型的,只能在具体的代码里面进行检查,functools提供了一个装饰器,可以去做这样的类型检查.一个简单的例子import functools@functools.singledispatchdef myfunc(args): print(args)@myfunc.register(list)def myfunc_list(args): for i in args: print(“List item: {}".format(i))if name == “main”: # 传入两个不同的类型参数,其处理逻辑也是不同 myfunc([1,2,3,6,4,5]) # 但是其调用的接口是一样的。 这样做的好处是可以帮助我们分离代码逻辑 myfunc(“Hello World”)输出:List item: 1List item: 2List item: 3List item: 6List item: 4List item: 5Hello World