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

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.lryc.cn/news/237315.html

相关文章:

  • 【多线程 - 11、死锁】
  • flask实现session开发
  • paddle dataset
  • 接口自动化测试实战:JMeter+Ant+Jenkins+钉钉机器人群通知完美结合
  • HAL库STM32串口开启DMA接收数据
  • Web安全研究(五)
  • 2023.11.17-hive调优的常见方式
  • ts 联合react 实现ajax的封装,refreshtoken的功能
  • CISP模拟试题(一)
  • 轻量封装WebGPU渲染系统示例<35>- HDR环境数据应用到PBR渲染材质
  • 春秋云境靶场CVE-2022-28512漏洞复现(sql手工注入)
  • 数字化文化的守护之星:十八数藏的非遗创新之道
  • [机缘参悟-119] :反者道之动与阴阳太极
  • Docker搭建Redis集群
  • 学习Opencv(蝴蝶书/C++)代码——2.OpenCV初探
  • 基于AVR单片机的便携式心电监测设备设计与实现
  • 微机原理_14
  • 【Flink】核心概念:并行度与算子链
  • milvus采坑一:启动服务就会挂掉
  • WPF Visual, UIElement, FrameworkElement, Control这些类的区别
  • Python-----PyInstaller的简单使用
  • 8 Redis与Lua
  • 10个令人惊叹的Go语言技巧,让你的代码更加优雅
  • vue3 setup展示数据
  • 原理Redis-Dict字典
  • 卷积神经网络(VGG-19)灵笼人物识别
  • MQTT协议详解
  • WordPress画廊插件Envira Gallery v1.9.7河蟹版下载
  • 认识前端包常用包管理工具(npm、cnpm、pnpm、nvm、yarn)
  • 使用树莓派学习Linux系统编程的 --- 库编程(面试重点)