当前位置: 首页 > 编程笔记 >

Python中functools模块的常用函数解析

端木兴国
2023-03-14
本文向大家介绍Python中functools模块的常用函数解析,包括了Python中functools模块的常用函数解析的使用技巧和注意事项,需要的朋友参考一下

1.partial
首先是partial函数,它可以重新绑定函数的可选参数,生成一个callable的partial对象

>>> int('10') # 实际上等同于int('10', base=10)和int('10', 10) 
10 
>>> int('10', 2) # 实际上是int('10', base=2)的缩写 
2 
>>> from functools import partial 
>>> int2 = partial(int, 2) # 这里我没写base,结果就出错了 
>>> int2('10') 
Traceback (most recent call last): 
 File "<stdin>", line 1, in <module> 
TypeError: an integer is required 
>>> int2 = partial(int, base=2) # 把base参数绑定在int2这个函数里 
>>> int2('10') # 现在缺省参数base被设为2了 
2 
>>> int2('10', 3) # 没加base,结果又出错了 
Traceback (most recent call last): 
 File "<stdin>", line 1, in <module> 
TypeError: keyword parameter 'base' was given by position and by name 
>>> int2('10', base=3) 
3 
>>> type(int2) 
<type 'functools.partial'> 

从中可以看出,唯一要注意的是可选参数必须写出参数名。

2.update_wrapper
接着是update_wrapper函数,它可以把被封装函数的__name__、__module__、__doc__和 __dict__都复制到封装函数去:

#-*- coding: gbk -*- 
 
def thisIsliving(fun): 
 def living(*args, **kw): 
  return fun(*args, **kw) + '活着就是吃嘛。' 
 return living 
 
@thisIsliving 
def whatIsLiving(): 
 "什么是活着" 
 return '对啊,怎样才算活着呢?' 
 
print whatIsLiving() 
print whatIsLiving.__doc__ 
 
print 
 
from functools import update_wrapper 
def thisIsliving(fun): 
 def living(*args, **kw): 
  return fun(*args, **kw) + '活着就是吃嘛。' 
 return update_wrapper(living, fun) 
 
@thisIsliving 
def whatIsLiving(): 
 "什么是活着" 
 return '对啊,怎样才算活着呢?' 
 
print whatIsLiving() 
print whatIsLiving.__doc__ 

结果:

对啊,怎样才算活着呢?活着就是吃嘛。
None

对啊,怎样才算活着呢?活着就是吃嘛。
什么是活着

不过也没多大用处,毕竟只是少写了4行赋值语句而已。

3.wraps
再有是wraps函数,它将update_wrapper也封装了进来:

#-*- coding: gbk -*- 
 
from functools import wraps 
 
def thisIsliving(fun): 
 @wraps(fun) 
 def living(*args, **kw): 
  return fun(*args, **kw) + '活着就是吃嘛。' 
 return living 
 
@thisIsliving 
def whatIsLiving(): 
 "什么是活着" 
 return '对啊,怎样才算活着呢?' 
 
print whatIsLiving() 
print whatIsLiving.__doc__ 

结果还是一样的:

对啊,怎样才算活着呢?活着就是吃嘛。
什么是活着

4.total_ordering
最后至于total_ordering函数则给予类丰富的排序方法,使用装饰器简化了操作。如果使用必须在类里面定义一个__lt__(),__le__(), __gt__(), 或__ge__()。应该给类添加一个__eq__() 方法。

from functools import total_ordering

@total_ordering
class Student(object):
  def __init__(self, name):
    self.name = name

  def __eq__(self, other):
    return self.name.lower() == other.name.lower()

  def __lt__(self, other):
    return self.name.lower() < other.name.lower()

a = Student('dan')
b = Student('mink')

print a > b
print a
print sorted([b, a])

打印结果

False
<__main__.Student object at 0x7f16ecb194d0>
[<__main__.Student object at 0x7f16ecb194d0>, <__main__.Student object at 0x7f16ecb195d0>]

 类似资料:
  • 本文向大家介绍Python中functools模块函数解析,包括了Python中functools模块函数解析的使用技巧和注意事项,需要的朋友参考一下 Python自带的 functools 模块提供了一些常用的高阶函数,也就是用于处理其它函数的特殊函数。换言之,就是能使用该模块对可调用对象进行处理。 functools模块函数概览 functools.cmp_to_key(func) funct

  • 本文向大家介绍Python的math模块中的常用数学函数整理,包括了Python的math模块中的常用数学函数整理的使用技巧和注意事项,需要的朋友参考一下 在数学之中,除了加减乘除四则运算之外——这是小学数学——还有其它更多的运算,比如乘方、开方、对数运算等等,要实现这些运算,需要用到 Python 中的一个模块:Math 模块(module)是 Python 中非常重要的东西,你可以把它理解为

  • 本文向大家介绍python中itertools模块zip_longest函数详解,包括了python中itertools模块zip_longest函数详解的使用技巧和注意事项,需要的朋友参考一下 最近在看流畅的python,在看第14章节的itertools模块,对其itertools中的相关函数实现的逻辑的实现 其中在zip_longest(it_obj1, ..., it_objN, fill

  • 本文向大家介绍Python常用数据分析模块原理解析,包括了Python常用数据分析模块原理解析的使用技巧和注意事项,需要的朋友参考一下 前言 python是一门优秀的编程语言,而是python成为数据分析软件的是因为python强大的扩展模块。也就是这些python的扩展包让python可以做数据分析,主要包括numpy,scipy,pandas,matplotlib,scikit-learn等等

  • 本文向大家介绍python re模块findall()函数实例解析,包括了python re模块findall()函数实例解析的使用技巧和注意事项,需要的朋友参考一下 本文研究的是re模块findall()函数的相关内容,首先看看实例代码: 按以上代码例子讲解: findall函数返回的总是正则表达式在字符串中所有匹配结果的列表,此处主要讨论列表中“结果”的展现方式,即findall中返回列表中每

  • 问题内容: 我想了解如何从导入的模块执行功能。 这是我到目前为止的位置。 app / mocking.py: app / my_module / init.py: 测试/模拟测试.py: 这不 符合 我的预期。“已修补”模块仅返回的未模拟值。如何模拟要导入到被测名称空间中的其他包中的方法? 问题答案: 当您从包中使用装饰器时,您 未在 修补名称空间(从本例中导入模块),而是在被测试的名称空间中对其