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

【Python知识宝库】Python中的装饰器:优雅地扩展函数功能


在这里插入图片描述

🎬 鸽芷咕:个人主页

 🔥 个人专栏: 《C++干货基地》《粉丝福利》

⛺️生活的理想,就是为了理想的生活!

文章目录

  • 前言
    • 一、装饰器的定义
    • 二、使用装饰器
    • 三、自定义装饰器
      • 1. 有参数的函数装饰器
      • 2. 装饰器链
    • 四、装饰器的进阶用法
      • 1. 使用`functools.wraps`
      • 2. 装饰器工厂
    • 五、总结

前言

Python中的装饰器是一种特殊的函数,它们可以用来修改其他函数的功能。装饰器本质上是一个返回函数的函数,它们允许我们在不修改原始函数代码的情况下,添加额外的功能。本文将介绍Python中装饰器的使用,以及如何创建和使用自定义装饰器。

一、装饰器的定义

装饰器是一种特殊的函数,它们接受一个函数作为参数,并返回一个新的函数。装饰器通常用来在运行时动态地给函数添加功能。

def decorator(func):def wrapper():print("Something is happening before the function is called.")func()print("Something is happening after the function is called.")return wrapper

二、使用装饰器

使用装饰器非常简单,你只需要将装饰器应用于你想要修改的函数上。

@decorator
def say_hello():print("Hello!")
say_hello()
# 输出:
# Something is happening before the function is called.
# Hello!
# Something is happening after the function is called.

三、自定义装饰器

你可以创建自己的装饰器来扩展函数的功能。

1. 有参数的函数装饰器

如果被装饰的函数接受参数,你需要在装饰器内部创建一个包装函数来接收这些参数。

def repeat decorater(func):def wrapper(*args, **kwargs):print("Something is happening before the function is called.")result = func(*args, **kwargs)print("Something is happening after the function is called.")return resultreturn wrapper
@repeat
def greet(name):print(f"Hello, {name}!")
greet("Alice")
# 输出:
# Something is happening before the function is called.
# Hello, Alice!
# Something is happening after the function is called.

2. 装饰器链

装饰器可以叠加使用,形成一个装饰器链。每个装饰器都会对函数进行一层包装。

def debug(func):def wrapper(*args, **kwargs):print(f"Calling function: {func.__name__}")return func(*args, **kwargs)return wrapper
@debug
@repeat
def greet(name):print(f"Hello, {name}!")
greet("Bob")
# 输出:
# Calling function: greet
# Something is happening before the function is called.
# Hello, Bob!
# Something is happening after the function is called.

四、装饰器的进阶用法

1. 使用functools.wraps

为了保留原始函数的元信息(如文档字符串和名字),可以使用functools.wraps装饰器。

from functools import wraps
def decorator(func):@wraps(func)def wrapper(*args, **kwargs):print("Something is happening before the function is called.")result = func(*args, **kwargs)print("Something is happening after the function is called.")return resultreturn wrapper
@decorator
def greet(name):"""Greet the person with their name."""print(f"Hello, {name}!")
print(greet.__name__)  # 输出: greet
print(greet.__doc__)   # 输出: Greet the person with their name.

2. 装饰器工厂

装饰器工厂是一个返回装饰器的函数,它可以接受参数来定制装饰器的行为。

def decorator_factory(prefix):def decorator(func):@wraps(func)def wrapper(*args, **kwargs):print(f"{prefix}: Before calling {func.__name__}")result = func(*args, **kwargs)print(f"{prefix}: After calling {func.__name__}")return resultreturn wrapperreturn decorator
@decorator_factory("DEBUG")
def greet(name):print(f"Hello, {name}!")
greet("Charlie")
# 输出:
# DEBUG: Before calling greet
# Hello, Charlie!
# DEBUG: After calling greet

五、总结

Python中的装饰器是一种强大且优雅的工具,它们允许我们在不修改原始函数的情况下扩展函数的功能。通过使用装饰器,我们可以编写出更加清晰、可维护的代码。本文介绍了装饰器的定义、使用、自定义以及进阶用法,希望这些内容能够帮助你更好地理解和利用Python中的装饰器。


http://www.mrgr.cn/news/22090.html

相关文章:

  • 技术点:tailwindcss还要chrome插件
  • 浙大数据结构:03-树3 Tree Traversals Again
  • idea如何配置模板
  • Leetcode面试经典150题-215.数组中的第K个最大元素
  • Java 面试题:Java的垃圾收集算法 --xunznux
  • java使用jfreechart生成图表
  • CSP 2023 提高级第一轮 CSP-S 2023初试题 完善程序第二题解析 未完
  • 无需更换摄像头,无需施工改造,降低智能化升级成本的智慧工业开源了
  • 设计模式-外观模式
  • 智能制造核心领域:自动化、物联网、大数据分析、人工智能在现代制造业中的应用与融合
  • 远程代码执行-Log4j2漏洞
  • 三相直流无刷电机(BLDC)控制算法实现:BLDC有感启动算法思路分析
  • 第二十四章 rust中的运算符重载
  • Kotlin reified改造JSON解析
  • Origin 2024中文版下载安装教程最新版百度网盘分享链接地址
  • vulhub Thinkphp5 2-rce远程代码执行漏洞
  • C++ STL 数据结构 vector基本用法
  • UnLua调用C++函数
  • 嵌入式秋招面试 学习 面试经验提醒和分享
  • 活期存款类型