【Python知识】Python进阶-什么是装饰器?

Python装饰器介绍

    • 概述
      • [1. 基本装饰器](#1. 基本装饰器)
      • [2. 带参数的装饰器](#2. 带参数的装饰器)
      • [3. 带参数的函数装饰器(使用`functools.wraps`)](#3. 带参数的函数装饰器(使用functools.wraps))
      • [4. 类装饰器](#4. 类装饰器)
      • [5. 方法装饰器(在类中使用)](#5. 方法装饰器(在类中使用))
    • 参考文献

概述

Python装饰器是一种高级功能,允许你在不修改函数或方法定义的情况下,为其添加额外的功能或行为。装饰器本质上是一个高阶函数,它接受一个函数作为参数,并返回一个新的函数(或可调用对象)。下面是一些常见的Python装饰器类型,以及每个类型的具体例子:

1. 基本装饰器

基本装饰器是最简单的装饰器类型,它可以在函数调用前后添加一些额外的逻辑。

python 复制代码
def my_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

@my_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.

2. 带参数的装饰器

装饰器也可以接受参数,这允许你创建更灵活的装饰器。

python 复制代码
def repeat(num_times):
    def decorator_repeat(func):
        def wrapper(*args, **kwargs):
            for _ in range(num_times):
                func(*args, **kwargs)
        return wrapper
    return decorator_repeat

@repeat(num_times=3)
def greet(name):
    print(f"Hello, {name}!")

greet("Alice")

输出:

复制代码
Hello, Alice!
Hello, Alice!
Hello, Alice!

3. 带参数的函数装饰器(使用functools.wraps

为了保持被装饰函数的元数据(如函数名、文档字符串等),可以使用functools.wraps装饰器。

python 复制代码
import functools

def debug(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        result = func(*args, **kwargs)
        print(f"{func.__name__}({args!r}, {kwargs!r}) = {result!r}")
        return result
    return wrapper

@debug
def add(a, b):
    return a + b

print(add(2, 3))

输出:

复制代码
add((2, 3), {}) = 5
5

4. 类装饰器

类装饰器与函数装饰器类似,但它们应用于类而不是函数。

python 复制代码
def singleton(cls):
    instances = {}
    def get_instance(*args, **kwargs):
        if cls not in instances:
            instances[cls] = cls(*args, **kwargs)
        return instances[cls]
    return get_instance

@singleton
class MyClass:
    def __init__(self, value):
        self.value = value

obj1 = MyClass(10)
obj2 = MyClass(20)

print(obj1.value)  # 输出: 10
print(obj2.value)  # 输出: 10,因为obj1和obj2是同一个实例

5. 方法装饰器(在类中使用)

虽然方法本质上也是函数,但直接在类中为方法定义装饰器需要稍微不同的语法。

python 复制代码
def method_decorator(func):
    def wrapper(self, *args, **kwargs):
        print("Method is being called")
        return func(self, *args, **kwargs)
    return wrapper

class MyClass:
    @method_decorator
    def my_method(self):
        print("Inside my_method")

obj = MyClass()
obj.my_method()

输出:

复制代码
Method is being called
Inside my_method

请注意,对于类中的方法,装饰器通常还需要接受self(或cls对于类方法)作为第一个参数。

这些例子展示了装饰器在Python中的多种用途。装饰器是Python中一个非常强大且灵活的工具,可以用来实现各种设计模式、日志记录、性能测量、事务管理等功能。

参考文献

【Python知识】Python面向对象编程知识

相关推荐
立莹Sir1 分钟前
Calendar类日期设置进位问题
java·开发语言
风逸hhh1 小时前
python打卡day46@浙大疏锦行
开发语言·python
火兮明兮1 小时前
Python训练第四十三天
开发语言·python
ascarl20102 小时前
准确--k8s cgroup问题排查
java·开发语言
互联网杂货铺2 小时前
完美搭建appium自动化环境
自动化测试·软件测试·python·测试工具·职场和发展·appium·测试用例
Gyoku Mint3 小时前
机器学习×第二卷:概念下篇——她不再只是模仿,而是开始决定怎么靠近你
人工智能·python·算法·机器学习·pandas·ai编程·matplotlib
fpcc3 小时前
跟我学c++中级篇——理解类型推导和C++不同版本的支持
开发语言·c++
莱茵菜苗3 小时前
Python打卡训练营day46——2025.06.06
开发语言·python
爱学习的小道长3 小时前
Python 构建法律DeepSeek RAG
开发语言·python
luojiaao4 小时前
【Python工具开发】k3q_arxml 简单但是非常好用的arxml编辑器,可以称为arxml杀手包
开发语言·python·编辑器