【Python】Python 装饰器的用法总结

发布于:2025-05-24 ⋅ 阅读:(20) ⋅ 点赞:(0)

在 Python 中,装饰器(Decorator) 是一种设计模式,用于在不修改函数或类代码的情况下动态地扩展其功能。装饰器广泛应用于日志记录、性能监控、权限验证等场景,提供了一种简洁优雅的方式来“包裹”现有的代码。本文将介绍装饰器的基本概念、使用方式及常见场景。


1. 什么是装饰器?

装饰器是一个 高阶函数,它接受一个函数或类作为输入,并返回一个新的函数或类,通常用于增加额外的功能。通过装饰器,我们可以在不修改原始代码的情况下扩展函数或类的行为。装饰器的核心思想是 “包装”,它用来增强或修改目标函数或类的功能。

2. 装饰器的基本语法

装饰器的基本语法形式是:

@decorator_function
def target_function():
    pass

等价于:

def target_function():
    pass

target_function = decorator_function(target_function)

3. 创建一个简单的函数装饰器

函数装饰器的实现通常包含两个函数:一个是装饰器本身,另一个是“包装函数”。包装函数会在原始函数执行前后执行自定义逻辑。

示例:简单的日志装饰器
def log_decorator(func):
    def wrapper(*args, **kwargs):
        print(f"Calling function: {func.__name__}")
        result = func(*args, **kwargs)
        print(f"Function {func.__name__} finished")
        return result
    return wrapper

@log_decorator
def say_hello(name):
    print(f"Hello, {name}!")

say_hello("Alice")

输出:

Calling function: say_hello
Hello, Alice!
Function say_hello finished

解释:

  • log_decorator 是装饰器,它接收 say_hello 函数作为参数,返回一个新的函数 wrapperwrapper 在执行 say_hello 之前和之后打印日志。

4. 带参数的装饰器

装饰器本身可以带参数,这时需要增加额外的嵌套层级。这样可以让装饰器在使用时更加灵活。

示例:带参数的装饰器
def repeat_decorator(repeat_count):
    def decorator(func):
        def wrapper(*args, **kwargs):
            for _ in range(repeat_count):
                result = func(*args, **kwargs)
            return result
        return wrapper
    return decorator

@repeat_decorator(repeat_count=3)
def say_hello(name):
    print(f"Hello, {name}!")

say_hello("Alice")

输出:

Hello, Alice!
Hello, Alice!
Hello, Alice!

解释:

  • repeat_decorator 装饰器可以接受一个 repeat_count 参数,表示函数执行的次数。在 wrapper 函数中,调用目标函数多次。

5. 类装饰器

除了函数装饰器,Python 也支持类装饰器,用于修改或增强类的行为。

示例:类装饰器
def add_method(cls):
    def new_method(self):
        print("This is a new method")
    cls.new_method = new_method
    return cls

@add_method
class MyClass:
    def greet(self):
        print("Hello!")

obj = MyClass()
obj.greet()      # 调用原方法
obj.new_method() # 调用新添加的方法

输出:

Hello!
This is a new method

解释:

  • add_method 装饰器给 MyClass 类添加了一个新的方法 new_method。通过 @add_method 装饰器,类 MyClass 被修改,新增了一个方法。

6. 使用 functools.wraps 保持原函数属性

当我们使用装饰器时,目标函数的元数据(如函数名、文档字符串等)会被包装函数覆盖。为了保留原函数的属性,我们可以使用 functools.wraps 装饰器。

示例:使用 wraps 保持元数据
from functools import wraps

def log_decorator(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        print(f"Calling function: {func.__name__}")
        return func(*args, **kwargs)
    return wrapper

@log_decorator
def say_hello(name):
    """Say hello to someone."""
    print(f"Hello, {name}!")

print(say_hello.__name__)  # 输出函数的名字
print(say_hello.__doc__)   # 输出函数的文档字符串

输出:

say_hello
Say hello to someone.

解释:

  • 使用 @wraps(func) 保证了装饰器函数 wrapper 保留了原始函数 say_hello 的名字和文档字符串。

7. 常见应用场景

7.1 缓存结果(Memoization)

装饰器可以用于缓存函数的返回结果,避免重复计算,提升性能。

示例:缓存装饰器
def memoize(func):
    cache = {}
    def wrapper(*args):
        if args not in cache:
            cache[args] = func(*args)
        return cache[args]
    return wrapper

@memoize
def slow_function(x):
    print(f"Computing {x}...")
    return x * x

print(slow_function(5))
print(slow_function(5))  # 缓存值,不会重复计算

输出:

Computing 5...
25
25
7.2 权限验证

装饰器常用于在执行函数前进行权限验证或用户身份检查。

示例:权限验证装饰器
def requires_permission(func):
    def wrapper(user, *args, **kwargs):
        if not user.get("is_admin", False):
            raise PermissionError("User does not have permission")
        return func(user, *args, **kwargs)
    return wrapper

@requires_permission
def delete_user(user, username):
    print(f"Deleting user {username}")

user = {"name": "Alice", "is_admin": False}
delete_user(user, "Bob")  # 抛出权限错误

8. 总结

装饰器是 Python 中的一项强大功能,能够让我们以非常简洁的方式在不改变原始代码的情况下增加功能。它们不仅适用于函数,也可以用于类、方法等。装饰器常常用于日志记录、缓存、权限验证等场景,在 Python 开发中十分常见。


网站公告

今日签到

点亮在社区的每一天
去签到