python functools.wraps保留被装饰函数属性

简介: python functools.wraps保留被装饰函数属性

作用

普通装饰器 ,会覆盖函数名称,并且 会替换 函数 文档字符串

介绍

functools.wraps(wrapped[, assigned][, updated])

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. For example:

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’
Without the use of this decorator factory, the name of the example function would have been ‘wrapper’, and the docstring of the original example() would have been lost.






相关文章
|
3天前
|
Python
python函数进阶
python函数进阶
|
3天前
|
安全 Python
Python量化炒股的获取数据函数—get_industry()
Python量化炒股的获取数据函数—get_industry()
10 3
|
3天前
|
Python
Python sorted() 函数和sort()函数对比分析
Python sorted() 函数和sort()函数对比分析
|
3天前
|
Python
Python量化炒股的获取数据函数—get_security_info()
Python量化炒股的获取数据函数—get_security_info()
10 1
|
3天前
|
存储 API 索引
让 Python 的属性查找具有 C 一级的性能
让 Python 的属性查找具有 C 一级的性能
7 0
|
3天前
|
Python
Python量化炒股的获取数据函数— get_billboard_list()
Python量化炒股的获取数据函数— get_billboard_list()
|
3天前
|
安全 数据库 数据格式
Python量化炒股的获取数据函数—get_fundamentals()
Python量化炒股的获取数据函数—get_fundamentals()
10 0
|
4天前
|
算法 Python
Python编程的函数—内置函数
Python编程的函数—内置函数
|
4天前
|
Java C++ Python
30天拿下Python之函数
30天拿下Python之函数
|
4天前
|
Python
Python量化炒股的获取数据函数—get_index_weights()
Python量化炒股的获取数据函数—get_index_weights()
13 0
下一篇
无影云桌面