Python装饰器(Decorators)深度解析
Python装饰器Decorators深度解析作为一名从后端开发转向Rust的开发者我发现Python的装饰器与Rust的特质Traits有一些相似之处它们都可以用于扩展代码的功能。今天我想分享一下我对Python装饰器的理解和实践。什么是装饰器装饰器是一种特殊的函数它可以修改其他函数或类的行为。装饰器的语法使用符号放在被装饰函数或类的定义之前。decorator def function(): pass这相当于def function(): pass function decorator(function)装饰器的基本实现1. 简单装饰器def simple_decorator(func): def wrapper(): print(Before function execution) func() print(After function execution) return wrapper simple_decorator def hello(): print(Hello, world!) # 调用函数 hello()输出Before function execution Hello, world! After function execution2. 带参数的装饰器def decorator_with_args(prefix): def decorator(func): def wrapper(): print(f{prefix}: Before function execution) func() print(f{prefix}: After function execution) return wrapper return decorator decorator_with_args(LOG) def hello(): print(Hello, world!) # 调用函数 hello()输出LOG: Before function execution Hello, world! LOG: After function execution3. 保留函数元数据的装饰器import functools def decorator_with_metadata(func): functools.wraps(func) def wrapper(): print(Before function execution) func() print(After function execution) return wrapper decorator_with_metadata def hello(): Print hello message print(Hello, world!) # 调用函数 hello() # 查看函数元数据 print(fFunction name: {hello.__name__}) print(fFunction docstring: {hello.__doc__})输出Before function execution Hello, world! After function execution Function name: hello Function docstring: Print hello message4. 带参数的函数装饰器import functools def decorator_with_args_and_function_args(func): functools.wraps(func) def wrapper(*args, **kwargs): print(Before function execution) result func(*args, **kwargs) print(After function execution) return result return wrapper decorator_with_args_and_function_args def add(a, b): Add two numbers return a b # 调用函数 result add(1, 2) print(fResult: {result}) # 查看函数元数据 print(fFunction name: {add.__name__}) print(fFunction docstring: {add.__doc__})输出Before function execution After function execution Result: 3 Function name: add Function docstring: Add two numbers装饰器的高级用法1. 类装饰器class ClassDecorator: def __init__(self, func): self.func func def __call__(self, *args, **kwargs): print(Before function execution) result self.func(*args, **kwargs) print(After function execution) return result ClassDecorator def hello(): print(Hello, world!) # 调用函数 hello()输出Before function execution Hello, world! After function execution2. 带参数的类装饰器class ClassDecoratorWithArgs: def __init__(self, prefix): self.prefix prefix def __call__(self, func): def wrapper(*args, **kwargs): print(f{self.prefix}: Before function execution) result func(*args, **kwargs) print(f{self.prefix}: After function execution) return result return wrapper ClassDecoratorWithArgs(LOG) def hello(): print(Hello, world!) # 调用函数 hello()输出LOG: Before function execution Hello, world! LOG: After function execution3. 装饰器链import functools def decorator1(func): functools.wraps(func) def wrapper(*args, **kwargs): print(Decorator 1: Before function execution) result func(*args, **kwargs) print(Decorator 1: After function execution) return result return wrapper def decorator2(func): functools.wraps(func) def wrapper(*args, **kwargs): print(Decorator 2: Before function execution) result func(*args, **kwargs) print(Decorator 2: After function execution) return result return wrapper decorator1 decorator2 def hello(): print(Hello, world!) # 调用函数 hello()输出Decorator 1: Before function execution Decorator 2: Before function execution Hello, world! Decorator 2: After function execution Decorator 1: After function execution4. 装饰器用于类方法import functools def method_decorator(func): functools.wraps(func) def wrapper(self, *args, **kwargs): print(Before method execution) result func(self, *args, **kwargs) print(After method execution) return result return wrapper class MyClass: method_decorator def hello(self): print(Hello, world!) # 创建实例并调用方法 obj MyClass() obj.hello()输出Before method execution Hello, world! After method execution装饰器的应用场景1. 日志记录import functools import time def log_execution(func): functools.wraps(func) def wrapper(*args, **kwargs): start_time time.time() print(fExecuting {func.__name__}...) result func(*args, **kwargs) end_time time.time() print(f{func.__name__} executed in {end_time - start_time:.4f} seconds) return result return wrapper
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2515227.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!