Python中functools模块的常用函数解析
作者:wyabc1986 时间:2022-08-12 08:10:50
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__
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
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
asp如何从数据库中调出个人档案?
2009-11-15 20:02:00
YOLOv8训练自己的数据集(详细教程)
2021-07-12 20:59:18
![](https://img.aspxhome.com/file/2023/6/96986_0s.jpg)
pytorch 实现张量tensor,图片,CPU,GPU,数组等的转换
2023-08-12 07:44:34
一段ASP单页显示文件夹下所有图片的代码
2011-02-28 11:23:00
python flask框架快速入门
2021-10-16 22:02:26
![](https://img.aspxhome.com/file/2023/0/89270_0s.jpg)
Python编程实现控制cmd命令行显示颜色的方法示例
2023-07-24 05:39:45
![](https://img.aspxhome.com/file/2023/0/60940_0s.jpg)
python3.4中清屏的处理方法
2023-11-14 04:09:21
![](https://img.aspxhome.com/file/2023/3/64603_0s.jpg)
文字适度阅读的宽度或者字数
2007-10-26 07:31:00
Django中URL的参数传递的实现
2022-12-24 13:53:25
django框架中ajax的使用及避开CSRF 验证的方式详解
2023-05-11 02:10:41
![](https://img.aspxhome.com/file/2023/9/80579_0s.png)
python实现经纬度采样的示例代码
2021-12-25 13:24:37
![](https://img.aspxhome.com/file/2023/7/90847_0s.png)
向Oracle数据库的CLOB属性插入数据报字符串过长错误
2023-07-23 11:11:06
ASP.NET获取不到JS设置cookies的解决方法
2023-07-19 08:07:08
内容为空时提交如何处理
2009-07-17 18:25:00
![](https://img.aspxhome.com/file/UploadPic/20097/17/twitter-400x94-35s.gif)
asp正则表达式详细说明
2008-10-23 16:23:00
Form表单及django的form表单的补充
2023-07-21 09:40:04
![](https://img.aspxhome.com/file/2023/4/99114_0s.png)
浅谈Golang内存逃逸
2023-08-27 07:51:55
ASP实现最简洁的多重查询的解决方案
2007-09-28 12:00:00
Pytorch中的 torch.distributions库详解
2021-05-17 22:26:47
![](https://img.aspxhome.com/file/2023/1/84081_0s.png)
SQL Server视图管理中的四个限制条件
2009-03-06 14:24:00