python functools
2013-02-25 17:02
274 查看
functools 是python2.5被引人的,文档在http://docs.python.org/library/functools.html
functools里有partial,reduce,update_wrapper,wraps
我们一个一个的来理解
1. functools.reduce和python内置的reduce是一样的,
2. functools.partial
一个带 n 个参数,partial 的函数固化第一个参数为固定参数,并返回另一个带 n-1 个参数函数对象.
3. update_wrapper
文档中是这样描述的
The main intended use for this function is in decorator functions
which wrap the decorated function and return the wrapper. If the wrapper function is not updated, the metadata of the returned function will reflect the wrapper definition rather than the original function definition, which is typically less than helpful.
4. functools.wraps,文档描叙
This is a convenience function for invoking partial(update_wrapper, wrapped=wrapped,assigned=assigned, updated=updated) as
a function decorator when defining a wrapper function.
其实也就是相当于在调用了partial,使用wraps可以把整个function的属性都带上。
另外python2.7里添加了cmp_to_key,total_ordering,我用的是2.6,这里就不谈论这个方法了。
http://www.cnblogs.com/twelfthing/articles/2145656.html
functools里有partial,reduce,update_wrapper,wraps
我们一个一个的来理解
1. functools.reduce和python内置的reduce是一样的,
2. functools.partial
一个带 n 个参数,partial 的函数固化第一个参数为固定参数,并返回另一个带 n-1 个参数函数对象.
from operator import add import functools print add(1,2) #3 add1 = functools.partial(add,1) print add1(10) #11
3. update_wrapper
文档中是这样描述的
The main intended use for this function is in decorator functions
which wrap the decorated function and return the wrapper. If the wrapper function is not updated, the metadata of the returned function will reflect the wrapper definition rather than the original function definition, which is typically less than helpful.
from functools import wraps from functools import update_wrapper def decorator0(f): def wrapper(*args, **kwds): print 'Calling decorated function---------------------0' return f(*args, **kwds) return wrapper def decorator1(f): def wrapper(*args, **kwds): print 'Calling decorated function---------------------1' return f(*args, **kwds) return wrapper @decorator0 def example(): """Docstring""" print 'Called example function' example() print '--------------------------------' update_wrapper(example,decorator1) example()
4. functools.wraps,文档描叙
This is a convenience function for invoking partial(update_wrapper, wrapped=wrapped,assigned=assigned, updated=updated) as
a function decorator when defining a wrapper function.
其实也就是相当于在调用了partial,使用wraps可以把整个function的属性都带上。
>>> from functools import wraps >>> def my_decorator(f): ... @wraps(f) ... def wrapper(*args, **kwds): ... print 'Calling decorated function' ... return f(*args, **kwds) ... return wrapper ... >>> @my_decorator ... def example(): ... """Docstring""" ... print 'Called example function' ... >>> example() Calling decorated function Called example function >>> example.__name__ 'example' >>> example.__doc__ 'Docstring'
另外python2.7里添加了cmp_to_key,total_ordering,我用的是2.6,这里就不谈论这个方法了。
http://www.cnblogs.com/twelfthing/articles/2145656.html
相关文章推荐
- python学习代码笔记(map,reduce,filter,sort,decorator,functools.partial)
- Python functools
- Python 装饰器之 functools.wraps
- Python:functools partial详解
- Python Intro - Functools Usage
- 用Python functools.wraps实现装饰器
- Python 入门:装饰器(decorator)、@functools.wraps、@staticmethod、@classmethod
- Python标准库之functools,itertools,operator
- Python中functools模块的常用函数解析
- PYTHON-进阶-FUNCTOOLS模块小结
- python 9-2 如何为被装饰的函数保存元数据,使用标准库functools中的装饰器wraps 装饰内部包裹函数
- python的functools.partial用法解释
- 【python】偏函数 functools partial
- 【Python模块】functools —— 作用于可调用对象的高阶函数集合
- Python 装饰器之 functools.wraps
- Python提供了以下几个内置tools模块:operator collections itertools functools
- python. partial应用from functools import partial
- Python技巧——list comprehension 与 functools.reduce
- 用Python functools.wraps实现装饰器
- python 初始认识重载操作符的利器:functools.total_ordering