functools 模块中有三个主要的函数 partial(), update_wrapper() 和 wraps()。

1、partial(func[,args][, *keywords])

  functools.partial 通过包装手法,允许我们 "重新定义" 函数签名。用一些默认参数包装一个可调用对象,返回结果是可调用对象,并且可以像原始对象一样对待冻结部分函数位置函数或关键字参数,简化函数,更少更灵活的函数参数调用。

from functools import partial
int2 = partial(int, base=2)print int2('11') # 3
print int2('101') # 5

当函数的参数个数太多,需要简化时,使用functools.partial可以创建一个新的函数,这个新函数可以固定住原函数的部分参数,从而在调用时更简单。

2、functool.update_wrapper

  默认partial对象没有__name__和__doc__, 这种情况下,对于装饰器函数非常难以debug.使用update_wrapper(),从原始对象拷贝或加入现有partial对象。它可以把被封装函数的__name__、module、__doc__和 __dict__都复制到封装函数去(模块级别常量WRAPPER_ASSIGNMENTS, WRAPPER_UPDATES)。

  这个函数主要用在装饰器函数中,装饰器返回函数反射得到的是包装函数的函数定义而不是原始函数定义

>>> functools.WRAPPER_ASSIGNMENTS
('__module__', '__name__', '__doc__')
>>> functools.WRAPPER_UPDATES
('__dict__',)

#!/usr/bin/env python
# encoding: utf-8def wrap(func):def call_it(*args, **kwargs):"""wrap func: call_it"""print 'before call'return func(*args, **kwargs)return call_it@wrap
def hello():"""say hello"""print 'hello world'from functools import update_wrapper
def wrap2(func):def call_it(*args, **kwargs):"""wrap func: call_it2"""print 'before call'return func(*args, **kwargs)return update_wrapper(call_it, func)@wrap2
def hello2():"""test hello"""print 'hello world2'if __name__ == '__main__':hello()print hello.__name__print hello.__doc__printhello2()print hello2.__name__print hello2.__doc__结果:
before call
hello world
call_it
wrap func: call_itbefore call
hello world2
hello2
test hello

3、functool.wraps

调用函数装饰器partial(update_wrapper, wrapped=wrapped, assigned=assigned, updated=updated)的简写。

from functools import wraps
def wrap3(func):@wraps(func)def call_it(*args, **kwargs):"""wrap func: call_it2"""print 'before call'return func(*args, **kwargs)return call_it@wrap3
def hello3():"""test hello 3"""print 'hello world3'结果:
before call
hello world3
hello3
test hello 3

详见:http://www.wklken.me/posts/2013/08/18/python-extra-functools.html

转载于:https://www.cnblogs.com/The-cup/p/4457360.html

Python常用模块之四 funsctools相关推荐

  1. 实战篇一 python常用模块和库介绍

    # -_-@ coding: utf-8 -_-@ -- Python 常用模块和库介绍 第一部分:json模块介绍 import json 将一个Python数据结构转换为JSON: dict_ = ...

  2. python常用模块大全总结-常用python模块

    广告关闭 2017年12月,云+社区对外发布,从最开始的技术博客到现在拥有多个社区产品.未来,我们一起乘风破浪,创造无限可能. python常用模块什么是模块? 常见的场景:一个模块就是一个包含了py ...

  3. 对于python来说、一个模块就是一个文件-python常用模块

    python常用模块 什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用pyt ...

  4. python常用模块之shelve模块

    python常用模块之shelve模块 shelve模块是一个简单的k,v将内存中的数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据类型 我们在上面讲json.pickle ...

  5. Python常用模块——目录

    Python常用模块学习 Python模块和包 Python常用模块time & datetime &random 模块 Python常用模块os & sys & sh ...

  6. Python常用模块集锦

    常用模块主要分为以下几类(缺失的后续再补充): 时间转换 时间计算 序列化和反序列化:json,pickle 编解码:unicode,base64 加解密:md5,sha1,hmac_sha1,aes ...

  7. Python+常用模块(2).md

    Python 常用模块 1. random模块 1.1 导入模块 import random 1.2 random.random() 生成一个从0到1的随机浮点数 1.3 random.uniform ...

  8. python用什么来写模块-Python常用模块——模块介绍与导入

    Python常用模块--模块介绍与导入 一.什么是模块? 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护. 为了编写可维护的代码,我们把很多函数分组,分 ...

  9. Python 常用模块总结

    Python 常用模块总结 1.random 2.math 3.os 4.os.path 5.sys 6.hashlib 7.hmac 8.time 9.datetime 10.calendar 11 ...

最新文章

  1. MinkowskiPooling池化(下)
  2. 【Paper】2021_Observer-based distributed consensus for multi-agent systems with directed networks and
  3. js小笔记 -- let const的理解
  4. vi 编辑器跳转到指定行数
  5. java ftp 判断目录存在_java判断ftp目录是否存在的方法
  6. 三星系统和鸿蒙系统,又一设备直升鸿蒙系统,现有操作系统被抛弃,和三星的想法一样!...
  7. 五、手动取消ajax请求 解决重复发送请求问题
  8. python语音库_Python中的Python文本到语音
  9. queue,stack中的库函数
  10. 我就是这样顺利拿到腾讯和微软的offer,
  11. 电商专业术语中英文对照
  12. 面试常见SQL练习题
  13. c语言流水灯程序详细讲解,用c语言编写单片机流水灯程序详解
  14. c语言实现10个数由小到大排序(插入排序法)
  15. 原生js实现上拉加载
  16. python-opencv文件夹中所有视频按顺序截图片并按顺序命名
  17. 用FPGA做一个生日礼物
  18. [附源码]java毕业设计企业记账系统
  19. 在线html过滤,在线Html编辑器粘贴过滤技术详解(二)
  20. 本地localhost换成本地的IP地址后不能访问的原因

热门文章

  1. 【读书笔记】《怪诞行为学》丹·艾瑞里
  2. 20个计算机英语关键词,英语微课堂:20个专业体育英语术语,秒懂赛场关键词!...
  3. Android SpannableString详细解析
  4. 软件测试之计算机基础
  5. 酷盘 文件服务器,文件上传云端 - 卡饭网
  6. 走亲访友(并查集的应用)
  7. 潮涌智造,后发的U9cloud能否挑战金蝶云星空霸主地位?
  8. BIO和NIO消耗的cpu和内存比较
  9. Visio Viewer 无法打开 VSD文件
  10. 苹果6怎么截屏_iPhone怎么设置双击截屏 苹果手机双击截屏图文教程