简介
在 Python 编程领域,装饰器是强大的工具,使开发者能够在不直接更改方法和函数源代码的情况下对其进行修改或增强。本教程将探讨将装饰器应用于方法的复杂性,深入全面地了解其实现、模式以及创建更灵活和可维护代码的最佳实践。
在 Python 编程领域,装饰器是强大的工具,使开发者能够在不直接更改方法和函数源代码的情况下对其进行修改或增强。本教程将探讨将装饰器应用于方法的复杂性,深入全面地了解其实现、模式以及创建更灵活和可维护代码的最佳实践。
Python 中的装饰器是一种强大的方式,用于在不直接更改函数和方法源代码的情况下对其进行修改或增强。它们本质上是将另一个函数作为参数,并返回该函数修改版本的函数。
def my_decorator(func):
def wrapper():
print("函数调用前的一些操作。")
func()
print("函数调用后的一些操作。")
return wrapper
@my_decorator
def say_hello():
print("你好!")
say_hello()
| 装饰器类型 | 描述 | 使用场景 |
|---|---|---|
| 函数装饰器 | 修改函数行为 | 日志记录、计时、认证 |
| 类装饰器 | 修改类行为 | 单例模式、类注册 |
| 方法装饰器 | 修改方法行为 | 缓存、验证 |
使用装饰器时,请注意:
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(3)
def greet(name):
print(f"你好,{name}!")
greet("LabEx 用户")
通过理解这些基础知识,你将为在 Python 编程之旅中有效地利用装饰器做好充分准备。
方法装饰器是专门用于类方法的装饰器,它提供了强大的方式来修改方法行为,而无需更改方法的内部实现。
class Calculator:
def __init__(self):
self.history = []
def log_method_call(func):
def wrapper(self, *args, **kwargs):
self.history.append(func.__name__)
return func(self, *args, **kwargs)
return wrapper
@log_method_call
def add(self, x, y):
return x + y
@log_method_call
def subtract(self, x, y):
return x - y
class DataProcessor:
total_instances = 0
@classmethod
def count_instances(cls, func):
def wrapper(*args, **kwargs):
cls.total_instances += 1
return func(*args, **kwargs)
return wrapper
@count_instances
def process_data(self, data):
return [x * 2 for x in data]
| 装饰器类型 | 作用域 | 使用场景 | 示例 |
|---|---|---|---|
| 实例方法装饰器 | 单个实例 | 日志记录、验证 | 方法调用跟踪 |
| 类方法装饰器 | 整个类 | 计数、跟踪 | 实例管理 |
| 静态方法装饰器 | 无实例引用 | 实用函数 | 数学运算 |
import functools
def method_timer(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
import time
start = time.time()
result = func(*args, **kwargs)
end = time.time()
print(f"{func.__name__} 耗时 {end - start} 秒")
return result
return wrapper
def require_authentication(method):
def wrapper(self, *args, **kwargs):
if not self.is_authenticated:
raise PermissionError("用户未认证")
return method(self, *args, **kwargs)
return wrapper
class UserService:
def __init__(self, is_authenticated=False):
self.is_authenticated = is_authenticated
@require_authentication
def sensitive_operation(self):
print("执行敏感操作")
functools.wraps 保留方法元数据通过掌握这些方法装饰器模式,LabEx 用户可以编写更优雅、可维护且功能增强的 Python 代码。
def memoize(func):
cache = {}
def wrapper(*args):
if args not in cache:
cache[args] = func(*args)
return cache[args]
return wrapper
class MathOperations:
@memoize
def fibonacci(self, n):
if n < 2:
return n
return self.fibonacci(n-1) + self.fibonacci(n-2)
import time
import functools
def performance_monitor(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
start_time = time.time()
result = func(*args, **kwargs)
end_time = time.time()
print(f"{func.__name__} 执行耗时 {end_time - start_time:.4f} 秒")
return result
return wrapper
class DataProcessor:
@performance_monitor
def process_large_dataset(self, dataset):
return [x * 2 for x in dataset]
| 模式 | 描述 | 使用场景 |
|---|---|---|
| 日志记录装饰器 | 跟踪方法调用 | 调试、审计 |
| 验证装饰器 | 检查输入参数 | 数据完整性 |
| 重试装饰器 | 处理临时故障 | 网络操作 |
def retry(max_attempts=3, delay=1):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
attempts = 0
while attempts < max_attempts:
try:
return func(*args, **kwargs)
except Exception as e:
attempts += 1
if attempts == max_attempts:
raise
time.sleep(delay)
return wrapper
return decorator
class NetworkService:
@retry(max_attempts=3, delay=2)
def fetch_data(self, url):
## 模拟网络请求
pass
def validate_inputs(input_type):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
for arg in args:
if not isinstance(arg, input_type):
raise TypeError(f"期望类型为 {input_type},得到的类型为 {type(arg)}")
return func(*args, **kwargs)
return wrapper
return decorator
class Calculator:
@validate_inputs(int)
def add_numbers(self, a, b):
return a + b
def context_manager(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
print("进入上下文")
try:
result = func(*args, **kwargs)
print("成功退出上下文")
return result
except Exception as e:
print(f"上下文中的错误: {e}")
raise
return wrapper
class ResourceManager:
@context_manager
def process_resource(self, resource):
## 资源处理逻辑
pass
functools.wraps 保留元数据functools.wraps 维护函数元数据通过掌握这些实际应用技术,LabEx 用户可以有效地使用装饰器创建强大、灵活且可维护的 Python 代码。
通过理解将装饰器应用于方法的细微技巧,Python 开发者可以显著提高代码的模块化、可读性和可扩展性。本教程涵盖了基本的装饰器模式、实际应用策略,并展示了装饰器如何在保持代码简洁高效的编程实践的同时改变方法的行为。