当前位置: 首页 > article >正文

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.


http://www.kler.cn/news/135130.html

相关文章:

  • 王颖奇:ONES.ai 上线,以及我的一些思考
  • josef约瑟 闭锁继电器 LB-7DG 100V 50HZ 导轨安装
  • Git常用指令-1
  • Spring Boot 项目的常用注解与依赖
  • Nginx快速入门教程,域名转发、负载均衡
  • 米贸搜| 关于Facebook企业户的那些事
  • 【C/C++】递归算法
  • el-table固定表头(设置height)出现内容过多时不能滚动问题
  • C# 依赖注入IServiceCollection
  • 制作Go程序的Docker容器(以及容器和主机的网络问题)
  • 使用持久卷部署 WordPress 和 MySQL
  • linux如何重置root密码
  • go语言学习-数据结构
  • 【计算机网络笔记】IPv6简介
  • linux高级篇基础理论五(用户安全,口令设置,JR暴力破解用户密码,NMAP端口扫描)
  • 精密云工程:智能激活业务速率 ——华为云11.11联合大促倒计时 仅剩3日
  • 未来之路:互联网技术驱动汽车行业的创新浪潮
  • Java集合大总结——List的简单使用
  • 大模型的语言能力
  • Kotlin原理+协程基本使用
  • 从能用到好用,国产CPU不是你想象中的样子了?
  • C语言入门笔记—static、extern、define、指针、结构体
  • Flink Operator 使用指南 之 Flink Operatoran安装
  • 思维导图软件 Xmind mac中文版特点介绍
  • Netty源码学习4——服务端是处理新连接的netty的reactor模式
  • 【数据结构初阶(3)】双向带头结点循环链表
  • JS模块引入方式使用 importScripts 替代 import
  • 冬季如何养胃?羊大师建议水果蔬菜不可少!
  • 学习教授LLM逻辑推理11.19
  • 用照片预测人的年龄【图像回归】