Python修饰器(带参数的修饰器)

理解Python装饰器
本文介绍Python装饰器的基本概念,包括不带参数及带参数的装饰器实现方式,并通过实例演示了如何使用装饰器来增强函数的功能。
def deco(func):
    def wrapper(*arg, **kw):
        print('decorator arg: %s' % arg_of_decorator)
        print('call %s in decorator' % func.__name__)
        return func(*arg, **kw)
    return wrapper

@deco
def test():
    pass

#等价于 test=deco(test),即在@修饰器名称后直接加(被修饰的函数名)
#因此,带参数的修饰器可以如下实现:


def decorator(arg_of_decorator):
    def deco(func):
        def wrapper(*arg, **kw):
            print('decorator arg: %s' % arg_of_decorator)
            print('call %s in decorator' % func.__name__)
            return func(*arg, **kw)
        return wrapper
    return deco


@decorator("修饰器参数")
def test1():
    pass
#等价于 test1 = decorator("修饰器参数")(test1) = deco(test1)
#修饰器的参数已经传递进去


### Python装饰器的使用和原理 Python装饰器是一种特殊类型的函数,它可以修改其他函数或类的行为,而无需修改它们的源代码。装饰器本质上是一个接受函数作为参数的函数,并返回一个新的函数。这种机制使得可以在不改变原有代码结构的情况下,为函数添加新的功能。 装饰器的核心原理是函数的嵌套和闭包。当使用 `@decorator` 语法时,Python 会将被装饰的函数作为参数传递给装饰器函数,并将装饰器返回的新函数替换原来的函数。例如,以下代码展示了一个简单的装饰器,在执行目标函数之前打印一条消息: ```python def decorator(func): def wrapper(): print("Executing before the function...") func() return wrapper @decorator def hello(): print("Hello, world!") hello() ``` 运行结果如下: ``` Executing before the function... Hello, world! ``` 在此示例中,`hello` 函数被 `decorator` 装饰器包裹,`hello` 实际上指向了 `wrapper` 函数,而 `wrapper` 在调用 `func()` 之前执行了额外的操作[^2]。 ### 装饰器的进阶用法 装饰器不仅可以用于简单的函数包装,还可以带参数,甚至可以装饰带有参数的函数。以下是一个能够处理任意参数的装饰器示例: ```python def decorator_with_args(arg): def decorator(func): def wrapper(*args, **kwargs): print(f"Decorator argument: {arg}") print("Executing before the function...") result = func(*args, **kwargs) print("Executing after the function...") return result return wrapper return decorator @decorator_with_args("test") def greet(name): print(f"Hello, {name}!") greet("Alice") ``` 运行结果如下: ``` Decorator argument: test Executing before the function... Hello, Alice! Executing after the function... ``` 在这个例子中,`decorator_with_args` 是一个带有参数的装饰器工厂函数,它返回一个真正的装饰器 `decorator`,而 `decorator` 返回的 `wrapper` 函数则处理了任意数量的位置参数和关键字参数[^4]。 ### 装饰器的实际应用 装饰器在实际开发中有广泛的应用场景,例如: - **权限控制**:可以使用装饰器来检查用户是否有权限执行某个操作。例如,Flask 框架中的 `@login_required` 装饰器可以限制只有登录用户才能访问某些视图函数。 - **性能测试**:可以使用装饰器来测量函数的执行时间,如下所示: ```python import time def timeit(func): def wrapper(*args, **kwargs): start_time = time.time() result = func(*args, **kwargs) end_time = time.time() print(f"Function {func.__name__} took {end_time - start_time:.4f} seconds to execute.") return result return wrapper @timeit def slow_function(): time.sleep(2) slow_function() ``` 运行结果如下: ``` Function slow_function took 2.0002 seconds to execute. ``` 此示例中的 `timeit` 装饰器可以自动记录被装饰函数的执行时间,非常适合用于性能调试[^3]。 ### 装饰器的链式调用 Python 还支持多个装饰器的链式调用。装饰器的执行顺序是从内到外,即最靠近函数的装饰器最先执行。例如: ```python def decorator1(func): def wrapper(*args, **kwargs): print("Decorator 1 before") result = func(*args, **kwargs) print("Decorator 1 after") return result return wrapper def decorator2(func): def wrapper(*args, **kwargs): print("Decorator 2 before") result = func(*args, **kwargs) print("Decorator 2 after") return result return wrapper @decorator1 @decorator2 def say_hello(): print("Hello") say_hello() ``` 运行结果如下: ``` Decorator 1 before Decorator 2 before Hello Decorator 2 after Decorator 1 after ``` 在这个例子中,`say_hello` 函数首先被 `decorator2` 装饰,然后被 `decorator1` 装饰。因此,`decorator2` 的 `wrapper` 函数会在 `decorator1` 的 `wrapper` 函数内部被调用[^1]。 ### 总结 Python 装饰器是一种强大的工具,能够以非常简洁的方式扩展函数的功能。通过理解装饰器的实现原理和使用方式,可以写出更加模块化、可重用的代码。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值