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

Python 装饰器详解

装饰器是 Python 中一种强大的语法特性,它允许在不修改原函数代码的情况下动态地扩展函数的功能。装饰器本质上是一个高阶函数,它接受一个函数作为参数并返回一个新的函数。

基本装饰器

1. 简单装饰器示例

def my_decorator(func):def wrapper():print("函数执行前")func()print("函数执行后")return wrapper@my_decorator
def say_hello():print("Hello!")say_hello()

输出:

函数执行前
Hello!
函数执行后

2. 装饰器的工作原理

  • @my_decorator 语法糖等价于 say_hello = my_decorator(say_hello)
  • my_decorator 接收 say_hello 函数作为参数
  • 返回 wrapper 函数,它"包裹"了原函数

带参数的装饰器

1. 函数带参数的装饰器

def decorator_with_args(func):def wrapper(name):print(f"准备调用 {func.__name__}")func(name)print(f"{func.__name__} 调用完成")return wrapper@decorator_with_args
def greet(name):print(f"Hello, {name}!")greet("Alice")

2. 装饰器本身带参数

def repeat(num_times):def decorator(func):def wrapper(*args, **kwargs):for _ in range(num_times):func(*args, **kwargs)return wrapperreturn decorator@repeat(3)
def say_hello(name):print(f"Hello, {name}!")say_hello("Bob")

输出:

Hello, Bob!
Hello, Bob!
Hello, Bob!

类装饰器

装饰器不仅可以是函数,还可以是类:

class CountCalls:def __init__(self, func):self.func = funcself.num_calls = 0def __call__(self, *args, **kwargs):self.num_calls += 1print(f"函数已被调用 {self.num_calls} 次")return self.func(*args, **kwargs)@CountCalls
def example():print("执行示例函数")example()
example()
example()

输出:

函数已被调用 1 次
执行示例函数
函数已被调用 2 次
执行示例函数
函数已被调用 3 次
执行示例函数

多个装饰器叠加

可以同时应用多个装饰器:

def uppercase_decorator(func):def wrapper():result = func()return result.upper()return wrapperdef exclamation_decorator(func):def wrapper():result = func()return result + "!"return wrapper@exclamation_decorator
@uppercase_decorator
def say_hi():return "hi there"print(say_hi())  # 输出: HI THERE!

注意:装饰器的应用顺序是从下往上,先应用 uppercase_decorator,然后是 exclamation_decorator

内置装饰器

Python 本身提供了一些内置装饰器:

  1. @staticmethod - 定义静态方法
  2. @classmethod - 定义类方法
  3. @property - 将方法转换为属性

装饰器的常见用途

  1. 日志记录
  2. 性能测试(计算函数执行时间)
  3. 权限验证
  4. 缓存(备忘录模式)
  5. 输入验证
  6. 注册插件
  7. 重试机制

性能测试装饰器示例

import timedef timer(func):def wrapper(*args, **kwargs):start = time.time()result = func(*args, **kwargs)end = time.time()print(f"{func.__name__} 执行时间: {end - start:.4f} 秒")return resultreturn wrapper@timer
def slow_function():time.sleep(2)slow_function()

保留原函数的元信息

使用 functools.wraps 可以保留原函数的元信息:

from functools import wrapsdef my_decorator(func):@wraps(func)def wrapper(*args, **kwargs):"""这是包装函数的文档字符串"""print("装饰器添加的功能")return func(*args, **kwargs)return wrapper@my_decorator
def example():"""这是原函数的文档字符串"""print("原函数功能")print(example.__name__)  # 输出: example
print(example.__doc__)   # 输出: 这是原函数的文档字符串

总结

装饰器是 Python 中非常强大的特性,它:

  • 允许在不修改原函数代码的情况下扩展功能
  • 遵循开放-封闭原则(对扩展开放,对修改封闭)
  • 使代码更加模块化和可重用
  • 常用于日志、性能测试、权限控制等场景

掌握装饰器可以让你写出更加优雅和高效的 Python 代码。

http://www.xdnf.cn/news/7086.html

相关文章:

  • 提示工程 - 系统提示(System Prompts)
  • AI日报 - 2025年05月19日
  • Fine-Tuning Llama2 with LoRA
  • STC89C52单片机模拟实现洗衣机控制 Proteus仿真
  • TYUT-企业级开发教程-第一章
  • Science Robotics 封面论文:基于形态学开放式参数化的仿人灵巧手设计用于具身操作
  • 如何完美安装GPU版本的torch、torchvision----解决torch安装慢 无法安装 需要翻墙安装 安装的是GPU版本但无法使用的GPU的错误
  • C++:⾯向对象的三⼤特性
  • Java正则表达式:从基础到高级应用全解析
  • ColorAid —— 一个面向设计师的色盲模拟工具开发记
  • 超越想象:利用MetaGPT打造高效的AI协作环境
  • Vue 3 中使用 md-editor-v3 的完整实例markdown文本
  • Pandas 构建并评价聚类模型② 第六章
  • 实现菜谱二级联动导航
  • ubuntu防火墙命令和放行ssh端口
  • 03 Nginx日志格式及可视化
  • Estimation(估算):业务分析师的“不确定性对抗术”
  • LeetCode Hot100刷题——除自身以外数组的乘积
  • 【设计模式】- 行为型模式2
  • 时序数据库、实时数据库与实时数仓:如何为实时数据场景选择最佳解决方案?
  • 【Linux】第十八章 调优系统性能
  • 结构体对齐三大法则
  • UART、SPI、IIC复习总结
  • 获取Class的方式有哪些?
  • 蓝桥杯19681 01背包
  • 医学影像开发的开源生态与技术实践:从DCMTK到DICOMweb的全面探索
  • NC61 两数之和【牛客网】
  • 写spark程序数据计算( 数据库的计算,求和,汇总之类的)连接mysql数据库,写入计算结果
  • COCO数据集神经网络性能现状2025.5.18
  • 【数据结构】2-3-4 单链表的建立