python高阶函数functools模块如何使用


本篇内容主要讲解“python高阶函数functools模块如何使用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“python高阶函数functools模块如何使用”吧!

functools模块提供了一些常用的高阶函数(处理其他可调用对象/函数的特殊函数;以函数作为输入参数,返回也是函数)。

functools模块

functools模块中的高阶函数可基于已有函数定义新的函数:

  • cmp_to_key,

  • total_ordering,

  • reduce,

  • partial,

  • update_wrapper

  • wraps

reduce

reduce(function, iterable[, initializer])对一个可迭代数据集合中的所有数据进行累积。

  • function:接受两个参数的函数;

  • sequence:可迭代对象(tuple/list/dict/str);

  • initial:可选初始值;

#累加reduce(lambdax,y:x+y,[1,2,3,4])#10#逆序字符串reduce(lambdax,y:y+x,'abcdefg')#'gfedcba'

partial/partialmethod

partial用于"冻结"函数的部分参数,返回一个参数更少、使用更简单的函数对象。使用时,只需传入未冻结的参数即可。partialmethod用于处理类方法。

functools.partial(func[, *args][, **keywords])返回一个新的partial对象:

  • func:一个可调用的对象或函数;

  • args:要冻结的位置参数;

  • keywords:要冻结的关键字参数。

defadd(a,b,note="add"):result=a+bprint(f"{note}result:{result}")returnresultadd3=functools.partial(add,3)add5=functools.partial(add,5,note="partialed")print(add3(1))#addresult:4print(add3(2,note="partial3"))#partial3result:5print(add5(3))#partialedresult:8

partialmethod用于类中的方法

classCell(object):def__init__(self):self._alive=False@propertydefalive(self):returnself._alivedefset_state(self,state):self._alive=bool(state)set_alive=functools.partialmethod(set_state,True)set_dead=functools.partialmethod(set_state,False)c=Cell()print(c.alive)#Falsec.set_alive()print(c.alive)#True

wraps/update_wrapper

functools.update_wrapper(wrapper, wrapped [, assigned] [, updated])更新一个包裹(wrapper)函数,使其看起来更像被包裹(wrapped)的函数(即把 被封装函数的__name__、__module__、__doc__和 __dict__都复制到封装函数去。wraps是通过partial与update_wrapper实现的。

通常,经由被装饰(decorator)的函数会表现为另外一个函数了(函数名、说明等都变为了装饰器的);通过wraps函数可以消除装饰器的这些副作用。

defwrapper_decorator(func):@functools.wraps(func)#若是去掉此wraps,则被装饰的函数名称与说明都变为此函数的defwrapper(*args,**kwargs):"""docofdecorator"""print('inwrapper_decorator...')returnfunc(*args,**kwargs)returnwrapper@wrapper_decoratordefexample():"""docofexample"""print('inexamplefunction')example()#inwrapper_decorator...#inexamplefunctionprint(example.__name__,";",example.__doc__)#example;docofexample

singledispatch/singledispatchmethod

singledispatch将普通函数转换为泛型函数,而singledispatchmethod(3.8引入)将类方法转换为泛型函数:

  • 泛型函数:是指由多个函数(针对不同类型的实现)组成的函数,调用时由分派算法决定使用哪个实现;

  • Single dispatch:一种泛型函数分派形式,基于第一个参数的类型来决定;

dispatch使用:

  • singledispatch装饰dispatch的基函数base_fun(即,注册object类型);

  • 注册后续分发函数使用装饰器@{base_fun}.register({type}),注册每种需要特殊处理的类型;

  • 分发函数名称无关紧要,_是个不错的选择;

  • 可以叠放多个register装饰器,让同一个函数支持多种类型;

#缺省匹配类型,注册object类型(与后续注册类型都不匹配时使用)@functools.singledispatchdefshow_dispatch(obj):print(obj,type(obj),"dispatcher")#匹配str字符串@show_dispatch.register(str)def_(text):print(text,type(text),"str")#匹配int@show_dispatch.register(int)def_(n):print(n,type(n),"int")#匹配元祖或者字典@show_dispatch.register(tuple)@show_dispatch.register(dict)def_(tup_dic):print(tup_dic,type(tup_dic),"tuple/dict")###打印注册的类型:#dict_keys([<class'object'>,<class'str'>,<class'int'>,<class'dict'>,<class'tuple'>])print(show_dispatch.registry.keys())show_dispatch(1)show_dispatch("xx")show_dispatch([1])show_dispatch((1,2,3))show_dispatch({"a":"b"})#1<class'int'>int#xx<class'str'>str#[1]<class'list'>dispatcher#(1,2,3)<class'tuple'>tuple/dict#{'a':'b'}<class'dict'>tuple/dict

cmp_to_key

cmp_to_key()用来自定义排序规则,可将比较函数parison function)转化为关键字函数(key function):

  • 比较函数:接受两个参数,比较这两个参数,并返回0、1或-1;

  • 关键字函数:接受一个参数,返回其对应的可比较对象;

test=[1,3,5,2,4]test.sort(key=functools.cmp_to_key(lambdax,y:1ifx<yelse-1))print(test)#[5,4,3,2,1]

total_ordering

是一个类装饰器,用于自动实现类的比较运算;类定义一个或者多个比较排序方法,类装饰器将会补充其余的比较方法。

被修饰的类必须至少定义 __lt__(), __le__(),__gt__(),__ge__()中的一个,以及__eq__()方法。

如,只需定义lt与eq方法,即可实现所有比较:

@functools.total_orderingclassPerson:def__init__(self,name,age):self.name=nameself.age=agedef__lt__(self,other):ifisinstance(other,Person):returnself.age<other.ageelse:raiseAttributeError("Incorrectattribute!")def__eq__(self,other):ifisinstance(other,Person):returnself.age==other.ageelse:raiseAttributeError("Incorrectattribute!")mike=Person("mike",20)tom=Person("tom",10)print(mike<tom)print(mike<=tom)print(mike>tom)print(mike>=tom)print(mike==tom)

到此,相信大家对“python高阶函数functools模块如何使用”有了更深的了解,不妨来实际操作一番吧!这里是主机评测网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!


上一篇:Python怎么绘制Matplotlib散点图

下一篇:python如何使用OpenCV实现多目标跟踪


Copyright © 2002-2019 测速网 https://www.inhv.cn/ 皖ICP备2023010105号 城市 地区 街道
温馨提示:部分文章图片数据来源与网络,仅供参考!版权归原作者所有,如有侵权请联系删除!
热门搜索