深入了解python装饰器(decorator)

什么是装饰器?

装饰器是 Python 中的一种高级功能,它允许你在不修改现有代码的情况下为函数或方法添加新的行为。装饰器本质上是一个接受函数并返回另一个函数的高阶函数。这使得它们非常适合用于横切关注点(如日志记录、权限检查、性能计时等),因为它们可以在不改变目标函数代码的前提下扩展其功能。

装饰器的基本用法

一个简单的装饰器如下所示:为say_hello函数添加打印执行前和执行后的提示信息

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.

在这个例子中,my_decorator 是一个装饰器函数,它接收一个函数 func 作为参数,并返回一个新的函数 wrapper@my_decorator 语法相当于 say_hello = my_decorator(say_hello)

带参数的装饰器

装饰器本身也可以接受参数。为此,我们需要再多包一层:
以下repeat装饰器让别装饰的函数执行指定次数

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!

在这个例子中,repeat 是一个接受参数的装饰器工厂,它返回一个实际的装饰器 decorator_repeat

装饰器与函数签名

为了让装饰器不改变被装饰函数的签名,可以使用 functools.wraps 来保留原函数的元数据:

import functools

def my_decorator(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        print("Before calling the function")
        result = func(*args, **kwargs)
        print("After calling the function")
        return result
    return wrapper

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

say_hello("Alice")
print(say_hello.__name__)

输出:

Before calling the function
Hello, Alice
After calling the function
say_hello

装饰器的实际应用

1. 日志记录

打印函数的接受的参数与返回值

def log_decorator(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        print(f"Calling {func.__name__} with args: {args}, kwargs: {kwargs}")
        result = func(*args, **kwargs)
        print(f"{func.__name__} returned {result}")
        return result
    return wrapper

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

add(2, 3)

输出:

Calling add with args: (2, 3), kwargs: {}
add returned 5

2. 权限检查

def requires_permission(permission):
    def decorator(func):
        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            if not user_has_permission(permission):
                raise PermissionError(f"User lacks {permission} permission")
            return func(*args, **kwargs)
        return wrapper
    return decorator

@requires_permission("admin")
def delete_user(user_id):
    print(f"User {user_id} deleted")

# Assuming user_has_permission("admin") returns False
delete_user(123)  # Raises PermissionError

3. 缓存

def cache(func):
    cached_results = {}
    @functools.wraps(func)
    def wrapper(*args):
        if args in cached_results:
            return cached_results[args]
        result = func(*args)
        cached_results[args] = result
        return result
    return wrapper

@cache
def fibonacci(n):
    if n in (0, 1):
        return n
    return fibonacci(n - 1) + fibonacci(n - 2)

print(fibonacci(10))

类装饰器

类也可以用作装饰器,实现更复杂的逻辑或状态管理:
使用类装饰器,记录函数被调用的次数

class CountCalls:
    def __init__(self, func):
        self.func = func
        self.num_calls = 0

    def __call__(self, *args, **kwargs):
        self.num_calls += 1
        print(f"Call {self.num_calls} of {self.func.__name__}")
        return self.func(*args, **kwargs)

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

say_hello()
say_hello()

输出:

Call 1 of say_hello
Hello!
Call 2 of say_hello
Hello!

总结

装饰器是 Python 中强大的工具,能够在不修改原函数代码的前提下添加额外的功能。通过理解装饰器的原理和使用方法,你可以编写更加灵活和可维护的代码。无论是日志记录、权限检查、性能计时还是缓存,装饰器都提供了一种优雅的解决方案。

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jesse_Kyrie

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值