python中的wraps函数
来源:互联网 发布:电脑windows无法启动32 编辑:程序博客网 时间:2024/05/24 00:13
使⽤装饰器时,有⼀些细节需要被注意。
例如,被装饰后的函数其实已经是另外⼀个函数了(函数名等函数属性会发⽣改变)。添加后由于函数名和函数的doc发⽣了改变,对测试结果有一定影响!
import functools
def itcast1(fun):
#带参数的装饰器
#wraps是用来将inner函数的属性设置为fun的属性值
@functools.wraps(fun)
def inner(*args, **kwargs):
print("itcast1 start")
fun(*args, **kwargs)
print("itcast1 end")
return inner
也可以用这种方法来表示:
# def itcast1(fun):
#
# definner(*args, **kwargs):
# print("itcast1 start")
# fun(*args, **kwargs)
# print("itcast1 end")
#
# inner.__name__ = fun.__name__
# inner.__doc__ = fun.__doc__
#
#
# returninner
@itcast1
def say_hello():
"""itcast funsay_hello"""
print("hello")
print(say_hello.__name__) # inner.__name__
print(say_hello.__doc__)
- python中的wraps函数
- 简单聊聊Python中的wraps修饰器
- python wraps decorator
- python 理解functools.wraps
- python @wraps修饰器
- python 9-2 如何为被装饰的函数保存元数据,使用标准库functools中的装饰器wraps 装饰内部包裹函数
- tensorflow编程: Wraps python functions
- python装饰器的wraps作用
- 用Python functools.wraps实现装饰器
- 用Python functools.wraps实现装饰器
- Python 装饰器之 functools.wraps
- python装饰器的wraps作用
- python中functools.wraps的作用
- Python 入门:装饰器(decorator)、@functools.wraps、@staticmethod、@classmethod
- functools.wraps
- functools.wraps
- functools.wraps
- [Python]Python函数参数中的*,**
- JMeter性能测试入门
- Latex中的cls文件使用(转)
- Java关键字static
- UML类图新手入门级介绍
- 383. Ransom Note(勒索信)
- python中的wraps函数
- CentOS 6.5 安装jdk和Tomcat
- SVG 可伸缩的矢量图形
- vb.net 教程 12-4 msHtml 2
- Tomcat 系统架构与设计模式之设计模式
- 多线程-李兴华
- 20170806
- 使用StarUML创建类图
- python中向类中动态添加新特性及删除属性方法