Python 装饰器详解:从入门到精通的 7 个实用案例

Python 装饰器详解:从入门到精通的 7 个实用案例

一、什么是装饰器

装饰器(Decorator)是 Python 中的一种高级特性,它允许你在不修改原函数代码的情况下,为函数添加额外的功能。简单来说,装饰器就是一个返回函数的函数。

二、基础语法

python 复制代码
def my_decorator(func):
    def wrapper():
        print("执行前")
        func()
        print("执行后")
    return wrapper

@my_decorator
def say_hello():
    print("Hello!")

say_hello()
# 输出:
# 执行前
# Hello!
# 执行后

三、带参数的装饰器

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

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

greet("World")

四、实用案例 1:日志记录

python 复制代码
import functools
import time

def logger(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        print(f"[LOG] 调用 {func.__name__}")
        result = func(*args, **kwargs)
        print(f"[LOG] {func.__name__} 完成")
        return result
    return wrapper

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

add(3, 5)

五、实用案例 2:性能计时

python 复制代码
def timer(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        start = time.time()
        result = func(*args, **kwargs)
        end = time.time()
        print(f"{func.__name__} 耗时:{end-start:.4f}秒")
        return result
    return wrapper

@timer
def slow_function():
    time.sleep(1)

slow_function()

六、实用案例 3:权限验证

python 复制代码
def require_auth(func):
    def wrapper(user, *args, **kwargs):
        if not user.is_authenticated:
            raise PermissionError("未授权访问")
        return func(user, *args, **kwargs)
    return wrapper

@require_auth
def delete_account(user):
    print(f"删除用户 {user.name} 的账户")

七、实用案例 4:缓存装饰器

python 复制代码
def cache(func):
    cache_dict = {}
    @functools.wraps(func)
    def wrapper(*args):
        if args in cache_dict:
            print("命中缓存")
            return cache_dict[args]
        result = func(*args)
        cache_dict[args] = result
        return result
    return wrapper

@cache
def fibonacci(n):
    if n < 2:
        return n
    return fibonacci(n-1) + fibonacci(n-2)

print(fibonacci(10))

八、实用案例 5:重试机制

python 复制代码
import random

def retry(max_attempts=3):
    def decorator(func):
        def wrapper(*args, **kwargs):
            for i in range(max_attempts):
                try:
                    return func(*args, **kwargs)
                except Exception as e:
                    if i == max_attempts - 1:
                        raise
                    print(f"重试 {i+1}/{max_attempts}")
            return None
        return wrapper
    return decorator

@retry(3)
def unstable_api():
    if random.random() < 0.7:
        raise ConnectionError("网络错误")
    return "成功"

九、实用案例 6:类型检查

python 复制代码
def type_check(*expected_types):
    def decorator(func):
        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            for arg, expected in zip(args, expected_types):
                if not isinstance(arg, expected):
                    raise TypeError(f"参数类型错误:期望 {expected}, 得到 {type(arg)}")
            return func(*args, **kwargs)
        return wrapper
    return decorator

@type_check(int, int)
def multiply(a, b):
    return a * b

multiply(5, 3)  # OK
# multiply("5", 3)  # TypeError

十、实用案例 7:类方法装饰器

python 复制代码
class MyClass:
    def __init__(self):
        self.count = 0
    
    def increment(func):
        def wrapper(self, *args, **kwargs):
            self.count += 1
            print(f"调用次数:{self.count}")
            return func(self, *args, **kwargs)
        return wrapper
    
    @increment
    def greet(self):
        print("Hello from MyClass!")

obj = MyClass()
obj.greet()
obj.greet()

总结

装饰器是 Python 中非常强大的工具,掌握它可以让你写出更优雅、更模块化的代码。以上 7 个案例涵盖了日志、性能监控、权限控制、缓存、重试、类型检查等常见场景,建议在实际项目中灵活运用。

相关推荐
sycmancia16 分钟前
Qt——编辑交互功能的实现
开发语言·qt
石山代码1 小时前
C++ 内存分区 堆区
java·开发语言·c++
前端若水1 小时前
会话管理:创建、切换、删除对话历史
前端·人工智能·python·react.js
无风听海1 小时前
C# 隐式转换深度解析
java·开发语言·c#
涛声依旧-底层原理研究所2 小时前
残差连接与层归一化通俗易懂的详解
人工智能·python·神经网络·transformer
一只大袋鼠2 小时前
Git 进阶(二):分支管理、暂存栈、远程仓库与多人协作
java·开发语言·git
csdn_aspnet2 小时前
Python 算法快闪 LeetCode 编号 70 - 爬楼梯
python·算法·leetcode·职场和发展
fantasy_arch2 小时前
pytorch人脸匹配模型
人工智能·pytorch·python
熊猫_豆豆2 小时前
广义相对论水星近日点进动完整详细数学推导
python·天体·广义相对论
LuminousCPP2 小时前
数据结构 - 线性表第四篇:C 语言通讯录优化升级全记录(踩坑 + 思考)
c语言·开发语言·数据结构·经验分享·笔记·学习