首页 后端开发 Python教程 Python中functools模块的常用函数解析

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

Mar 01, 2017 pm 01:50 PM
functools python

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(&#39;10&#39;) # 现在缺省参数base被设为2了 
2 
>>> int2(&#39;10&#39;, 3) # 没加base,结果又出错了 
Traceback (most recent call last): 
 File "<stdin>", line 1, in <module> 
TypeError: keyword parameter &#39;base&#39; was given by position and by name 
>>> int2(&#39;10&#39;, base=3) 
3 
>>> type(int2) 
<type &#39;functools.partial&#39;>
登录后复制

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

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

#-*- coding: gbk -*- 
 
def thisIsliving(fun): 
 def living(*args, **kw): 
  return fun(*args, **kw) + &#39;活着就是吃嘛。&#39; 
 return living 
 
@thisIsliving 
def whatIsLiving(): 
 "什么是活着" 
 return &#39;对啊,怎样才算活着呢?&#39; 
 
print whatIsLiving() 
print whatIsLiving.__doc__ 
 
print 
 
from functools import update_wrapper 
def thisIsliving(fun): 
 def living(*args, **kw): 
  return fun(*args, **kw) + &#39;活着就是吃嘛。&#39; 
 return update_wrapper(living, fun) 
 
@thisIsliving 
def whatIsLiving(): 
 "什么是活着" 
 return &#39;对啊,怎样才算活着呢?&#39; 
 
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) + &#39;活着就是吃嘛。&#39; 
 return living 
 
@thisIsliving 
def whatIsLiving(): 
 "什么是活着" 
 return &#39;对啊,怎样才算活着呢?&#39; 
 
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(&#39;dan&#39;)
b = Student(&#39;mink&#39;)

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模块的常用函数解析相关文章请关注PHP中文网!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
4 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

Linux系统自带Python解释器能删除吗? Linux系统自带Python解释器能删除吗? Apr 02, 2025 am 07:00 AM

关于Linux系统自带Python解释器的删除问题许多Linux发行版在安装时会预装Python解释器,它并非通过软件包管理器�...

如何解决Python中自定义装饰器的Pylance类型检测问题? 如何解决Python中自定义装饰器的Pylance类型检测问题? Apr 02, 2025 am 06:42 AM

使用自定义装饰器时的Pylance类型检测问题解决方法在Python编程中,装饰器是一种强大的工具,可以用于添加行�...

在Linux终端中使用python --version命令时如何解决权限问题? 在Linux终端中使用python --version命令时如何解决权限问题? Apr 02, 2025 am 06:36 AM

Linux终端中使用python...

如何使用Python的httpx库发送HTTP/2 POST请求? 如何使用Python的httpx库发送HTTP/2 POST请求? Apr 01, 2025 pm 11:54 PM

使用Python的httpx库发送HTTP/2...

Python 3.6加载pickle文件报错ModuleNotFoundError: No module named '__builtin__'怎么办? Python 3.6加载pickle文件报错ModuleNotFoundError: No module named '__builtin__'怎么办? Apr 02, 2025 am 06:27 AM

Python3.6环境下加载pickle文件报错:ModuleNotFoundError:Nomodulenamed...

FastAPI 和 aiohttp 是否共享同一个全局事件循环? FastAPI 和 aiohttp 是否共享同一个全局事件循环? Apr 02, 2025 am 06:12 AM

Python异步库之间的兼容性问题在Python中,异步编程已经成为处理高并发和I/O...

Python 3.6加载Pickle文件报错"__builtin__"模块未找到怎么办? Python 3.6加载Pickle文件报错"__builtin__"模块未找到怎么办? Apr 02, 2025 am 07:12 AM

Python3.6环境下加载Pickle文件报错:ModuleNotFoundError:Nomodulenamed...

See all articles