首页 后端开发 Python教程 Python 装饰器:综合指南

Python 装饰器:综合指南

Jan 05, 2025 pm 10:53 PM

Python Decorators: A Comprehensive Guide

当我开始使用Python编程时,如果我没记错的话,版本是3.3。因此,当我开始编程时,Python 社区很早就可以使用装饰器了。

Python 2.2 版本引入了函数装饰器,Python 2.6 版本引入了类装饰器。

就我个人而言,我认为 Python 的装饰器功能是该语言的一个非常强大的功能。

实际上,我的目标是制作一系列有关 Python 中最难理解的主题的文章。我打算一一涵盖这些主题,大概有十多个。

在本文中,我将尝试尽可能多地触及装饰器主题的每个部分。

1. 历史背景

  • 早期(Python 2.2 之前): 在装饰器之前,修改函数或类通常需要手动包装或猴子修补,这很麻烦且可读性较差。
  • 元类 (Python 2.2): 元类提供了一种控制类创建的方法,提供了装饰器稍后提供的一些功能,但对于简单的修改来说它们很复杂。
  • PEP 318 (Python 2.4): 装饰器通过 PEP 318 在 Python 2.4 中正式引入。该提案受到 Java 中注释的启发,旨在提供一种更清晰、更具声明性的方式来修改函数和方法.
  • 类装饰器 (Python 2.6): Python 2.6 扩展了对类的装饰器支持,进一步增强了类的多功能性。
  • 广泛采用: 装饰器很快成为一种流行的功能,广泛用于 Flask 和 Django 等框架中,用于路由、身份验证等。

2.什么是装饰器?

本质上,装饰器是Python中的一种设计模式,它允许您修改函数或类的行为而不改变其核心结构。装饰器是元编程的一种形式,您本质上是在编写操作其他代码的代码。

您知道 Python 使用以下顺序给出的范围来解析名称:

  1. 本地
  2. 附上
  3. 全球
  4. 内置

装饰器是坐封闭作用域,这与闭包概念密切相关。

关键思想:装饰器将函数作为输入,向其添加一些功能,然后返回修改后的函数。

类比:将装饰器视为礼品包装纸。您有一件礼物(原始功能),然后用装饰纸(装饰器)将其包裹起来,使其看起来更漂亮或添加额外的功能(例如蝴蝶结或卡片)。里面的礼物保持不变,但它的呈现或相关动作得到了增强。

A) 装饰器变体:基于函数与基于类

Python 中的大多数装饰器都是使用函数实现的,但您也可以使用类创建装饰器。
基于函数的装饰器更常见、更简单,而基于类的装饰器提供了额外的灵活性。

基于函数的基本装饰器语法

def my_decorator(func):
    def wrapper(*args, **kwargs):
        # Do something before calling the decorated function
        print("Before function call")
        result = func(*args, **kwargs)
        # Do something after calling the decorated function
        print("After function call")
        return result
    return wrapper

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

say_hello("World")
登录后复制
登录后复制

说明:

  • my_decorator 是装饰器函数。它需要将函数 func 修饰为输入。
  • 包装器是包装原始函数调用的内部函数。它可以在原始函数之前和之后执行代码。
  • @my_decorator 是装饰器语法。相当于 say_hello = my_decorator(say_hello).

基于类的基本装饰器语法

它们使用类而不是函数来定义装饰器。

class MyDecorator:
    def __init__(self, func):
        self.func = func

    def __call__(self, *args, **kwargs):
        # Do something before calling the decorated function
        print("Before function call")
        result = self.func(*args, **kwargs)
        # Do something after calling the decorated function
        print("After function call")
        return result

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

say_hello("World")
登录后复制
登录后复制

说明:

  • MyDecorator 是一个充当装饰器的类。
  • __init__方法存储要装饰的函数。
  • __call__ 方法使类实例可调用,允许它像函数一样使用。

B) 实现一个简单的装饰器

装饰器的基本概念是它们是将另一个函数作为参数并扩展其行为而不显式修改它的函数。
这是最简单的形式:

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

# Using the decorator with @ syntax
@my_decorator
def say_hello():
    print("Hello!")

# When we call say_hello()
say_hello()

# This is equivalent to:
# say_hello = my_decorator(say_hello)
登录后复制
登录后复制

C) 实现带有参数的装饰器

让我们创建一个记录函数执行时间的装饰器:

def decorator_with_args(func):
    def wrapper(*args, **kwargs):    # Accept any number of arguments
        print(f"Arguments received: {args}, {kwargs}")
        return func(*args, **kwargs)  # Pass arguments to the original function
    return wrapper

@decorator_with_args
def greet(name, greeting="Hello"):
    print(f"{greeting}, {name}!")

greet("Alice", greeting="Hi")  # Prints arguments then "Hi, Alice!"
登录后复制
登录后复制

D) 实现参数化装饰器

这些装饰器可以接受自己的参数:

def repeat(times):
    def decorator(func):
        def wrapper(*args, **kwargs):
            for _ in range(times):
                result = func(*args, **kwargs)
            return result
        return wrapper
    return decorator

@repeat(times=3)
def greet(name):
    print(f"Hello {name}")
    return "Done"

greet("Bob")  # Prints "Hello Bob" three times
登录后复制

E) 实现类装饰器

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 DatabaseConnection:
    def __init__(self):
        print("Initializing database connection")

# Creating multiple instances actually returns the same instance
db1 = DatabaseConnection()  # Prints initialization
db2 = DatabaseConnection()  # No initialization printed
print(db1 is db2)  # True
登录后复制

F) 实现方法装饰器

这些是专门为类方法设计的:

def debug_method(func):
    def wrapper(self, *args, **kwargs):
        print(f"Calling method {func.__name__} of {self.__class__.__name__}")
        return func(self, *args, **kwargs)
    return wrapper

class MyClass:
    @debug_method
    def my_method(self, x, y):
        return x + y

obj = MyClass()
print(obj.my_method(5, 3))

登录后复制

G) 实现装饰器链接

多个装饰器可以应用于单个函数:

def bold(func):
    def wrapper():
        return "<b>" + func() + "</b>"
    return wrapper

def italic(func):
    def wrapper():
        return "<i>" + func() + "</i>"
    return wrapper

@bold
@italic
def greet():
    return "Hello!"

print(greet())  # Outputs: <b><i>Hello!</i></b>
登录后复制

说明:

  • 装饰器是从下到上应用的。
  • 这更像是我们在数学中所做的:f(g(x))。
  • 首先应用斜体,然后应用粗体。

H)如果我们不使用 @functools.wraps 会发生什么?

functools.wraps 装饰器(请参阅文档)是一个辅助函数,当您使用装饰器包装原始函数时,它会保留原始函数的元数据(如其名称、文档字符串和签名)。如果您不使用它,您将丢失这些重要信息。

示例:

def my_decorator(func):
    def wrapper(*args, **kwargs):
        """Wrapper docstring"""
        return func(*args, **kwargs)
    return wrapper

@my_decorator
def my_function():
    """My function docstring"""
    pass

print(my_function.__name__)
print(my_function.__doc__)
登录后复制

输出:

wrapper
Wrapper docstring
登录后复制

问题:

  • 原始函数的名称(my_function)和文档字符串(“我的函数文档字符串”)丢失。
  • 这会使调试和自省变得困难。

解决方案:使用 functools.wraps):

def my_decorator(func):
    def wrapper(*args, **kwargs):
        # Do something before calling the decorated function
        print("Before function call")
        result = func(*args, **kwargs)
        # Do something after calling the decorated function
        print("After function call")
        return result
    return wrapper

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

say_hello("World")
登录后复制
登录后复制

输出:

class MyDecorator:
    def __init__(self, func):
        self.func = func

    def __call__(self, *args, **kwargs):
        # Do something before calling the decorated function
        print("Before function call")
        result = self.func(*args, **kwargs)
        # Do something after calling the decorated function
        print("After function call")
        return result

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

say_hello("World")
登录后复制
登录后复制

functools.wraps 的好处:

  • 保留函数元数据。
  • 提高代码可读性和可维护性。
  • 使调试更容易。
  • 帮助使用内省工具和文档生成器。

I) 具有状态的装饰器

装饰器还可以维护函数调用之间的状态。这对于缓存或计算函数调用等场景特别有用。

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

# Using the decorator with @ syntax
@my_decorator
def say_hello():
    print("Hello!")

# When we call say_hello()
say_hello()

# This is equivalent to:
# say_hello = my_decorator(say_hello)
登录后复制
登录后复制

输出:

def decorator_with_args(func):
    def wrapper(*args, **kwargs):    # Accept any number of arguments
        print(f"Arguments received: {args}, {kwargs}")
        return func(*args, **kwargs)  # Pass arguments to the original function
    return wrapper

@decorator_with_args
def greet(name, greeting="Hello"):
    print(f"{greeting}, {name}!")

greet("Alice", greeting="Hi")  # Prints arguments then "Hi, Alice!"
登录后复制
登录后复制

说明:

包装函数维护一个计数器(调用),每次调用装饰函数时该计数器都会递增。
这是一个如何使用装饰器来维护状态的简单示例。

J) Python 装饰器的最佳实践

  • 使用 functools.wraps: 始终在装饰器中使用 @functools.wraps 来保留原始函数的元数据。
  • 保持装饰器简单: 理想情况下,装饰器应该做一件特定的事情并且做得很好。这使得它们更可重用且更易于理解。
  • 记录你的装饰器:解释你的装饰器做了什么,它需要什么参数,以及它返回什么。
  • 测试你的装饰器:编写单元测试以确保你的装饰器在各种场景下按预期工作。
  • 考虑链接顺序:链接多个装饰器时请注意顺序,因为它会影响执行流程。

K) 糟糕的实现(反模式)

  • 过于复杂的装饰器: 避免创建过于复杂或尝试做太多事情的装饰器。这使得它们难以理解、维护和调试。
  • 忽略 functools.wraps: 忘记使用 @functools.wraps 会导致函数元数据丢失,从而导致自省和调试问题。
  • 副作用: 理想情况下,装饰器除了修改装饰函数之外不应产生意外的副作用。
  • 硬编码值: 避免在装饰器中对值进行硬编码。相反,使用装饰器工厂来使其可配置。
  • 未正确处理参数: 如果装饰器要与各种函数一起使用,请确保您的包装函数可以使用 *args 和 **kwargs 处理任意数量的位置和关键字参数。

L) 10. 现实世界用例

  • 日志记录: 记录函数调用、参数和返回值以进行调试或审核。
  • 计时: 测量函数的执行时间以进行性能分析。
  • 缓存: 存储昂贵的函数调用的结果以避免冗余计算(记忆)。
  • 身份验证和授权: 在执行函数之前验证用户凭据或权限。
  • 输入验证: 检查传递给函数的参数是否满足特定条件。
  • 速率限制: 控制在特定时间段内调用函数的次数。
  • 重试逻辑: 如果由于临时错误而失败,则自动重试函数调用。
  • 特定于框架的任务: Flask 和 Django 等框架使用装饰器进行路由(将 URL 映射到函数)、注册插件等。

M) Python 装饰器精选列表

您可以在下面找到 Python 装饰器的精选列表:

  • 很棒的 Python 装饰器
  • Python 装饰器库

N) 11. 结论

装饰器是 Python 中一个强大而优雅的功能,它允许您以干净和声明性的方式增强函数和类​​。
通过了解原理、最佳实践和潜在陷阱,您可以有效地利用装饰器来编写更加模块化、可维护和富有表现力的代码。
它们是任何 Python 程序员的武器库中的宝贵工具,特别是在使用框架或构建可重用组件时。

以上是Python 装饰器:综合指南的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

如何在使用 Fiddler Everywhere 进行中间人读取时避免被浏览器检测到? 如何在使用 Fiddler Everywhere 进行中间人读取时避免被浏览器检测到? Apr 02, 2025 am 07:15 AM

使用FiddlerEverywhere进行中间人读取时如何避免被检测到当你使用FiddlerEverywhere...

在Linux终端中使用python --version命令时如何解决权限问题? 在Linux终端中使用python --version命令时如何解决权限问题? Apr 02, 2025 am 06:36 AM

Linux终端中使用python...

如何在10小时内通过项目和问题驱动的方式教计算机小白编程基础? 如何在10小时内通过项目和问题驱动的方式教计算机小白编程基础? Apr 02, 2025 am 07:18 AM

如何在10小时内教计算机小白编程基础?如果你只有10个小时来教计算机小白一些编程知识,你会选择教些什么�...

如何绕过Investing.com的反爬虫机制获取新闻数据? 如何绕过Investing.com的反爬虫机制获取新闻数据? Apr 02, 2025 am 07:03 AM

攻克Investing.com的反爬虫策略许多人尝试爬取Investing.com(https://cn.investing.com/news/latest-news)的新闻数据时,常常�...

Python 3.6加载pickle文件报错ModuleNotFoundError: No module named '__builtin__'怎么办? Python 3.6加载pickle文件报错ModuleNotFoundError: No module named '__builtin__'怎么办? Apr 02, 2025 am 06:27 AM

Python3.6环境下加载pickle文件报错:ModuleNotFoundError:Nomodulenamed...

使用Scapy爬虫时,管道文件无法写入的原因是什么? 使用Scapy爬虫时,管道文件无法写入的原因是什么? Apr 02, 2025 am 06:45 AM

使用Scapy爬虫时管道文件无法写入的原因探讨在学习和使用Scapy爬虫进行数据持久化存储时,可能会遇到管道文�...

See all articles