装饰器是 Python 中的一种强大工具,可以为函数添加额外的功能,使代码更加优雅和高效。本文将介绍十个最佳的 Python 装饰器,它们可以增强你的函数功能。
1. @staticmethod
和 @classmethod
静态方法和类方法对于面向对象编程至关重要。它们允许你定义可以在类本身而不是类的实例上调用的方法。
<code>class MyClass: class_variable = "I am a class variable" @staticmethod def static_method(): print("This is a static method") @classmethod def class_method(cls): print(f"This is a class method. Accessing class variable: {cls.class_variable}")</code>
2. @property
@property
装饰器允许你定义可以像属性一样访问的方法,使代码更具可读性并保持封装性。
<code>class Circle: def __init__(self, radius): self._radius = radius @property def radius(self): return self._radius @radius.setter def radius(self, value): if value < 0: raise ValueError("Radius cannot be negative") self._radius = value</code>
3. @lru_cache
缓存对于优化具有昂贵计算量的函数调用至关重要。@lru_cache
装饰器缓存函数的结果,节省时间和资源。
<code>from functools import lru_cache @lru_cache(maxsize=None) def fibonacci(n): if n <= 1: return n return fibonacci(n-1) + fibonacci(n-2)</code>
4. @timer
对函数进行计时对于性能分析至关重要。@timer
装饰器可以测量函数的执行时间。
<code>import time def timer(func): def wrapper(*args, **kwargs): start_time = time.time() result = func(*args, **kwargs) end_time = time.time() print(f"{func.__name__} took {end_time - start_time} seconds to execute.") return result return wrapper @timer def slow_function(): time.sleep(2)</code>
5. @log_exception
调试可能是一项具有挑战性的任务。@log_exception
装饰器可帮助你记录函数内引发的异常,从而简化调试过程。
<code>def log_exception(func): def wrapper(*args, **kwargs): try: return func(*args, **kwargs) except Exception as e: print(f"Exception in {func.__name__}: {e}") return wrapper @log_exception def divide(a, b): return a / b</code>
6. @authenticated
安全性在许多应用中至关重要。@authenticated
装饰器可用于确保只有授权用户才能访问某些功能。
<code>def authenticated(func): def wrapper(user, *args, **kwargs): if user.is_authenticated: return func(user, *args, **kwargs) else: raise PermissionError("User is not authenticated") return wrapper @authenticated def access_sensitive_data(user): return "Sensitive data here"</code>
7. @validate_input
输入验证对于防止意外错误至关重要。@validate_input
装饰器可以检查和清理函数输入。
<code>def validate_input(*args_types): def decorator(func): def wrapper(*args, **kwargs): for arg, arg_type in zip(args, args_types): if not isinstance(arg, arg_type): raise ValueError(f"Invalid argument type for {arg}. Expected {arg_type}") return func(*args, **kwargs) return wrapper return decorator @validate_input(int, str) def example_function(age, name): print(f"Age: {age}, Name: {name}")</code>
8. @retry
在分布式系统和 API 的世界中,重试通常是必要的。@retry
装饰器可以自动重试一个函数,直到成功。
<code>import random def retry(max_attempts): def decorator(func): def wrapper(*args, **kwargs): attempts = 0 while attempts < max_attempts: try: return func(*args, **kwargs) except Exception as e: print(f"Attempt {attempts+1} failed with error: {e}") attempts += 1 delay = 2 ** attempts + random.uniform(0, 1) time.sleep(delay) raise Exception("Max retries reached") return wrapper return decorator @retry(max_attempts=3) def unstable_function(): if random.random() < 0.7: raise ValueError("Something went wrong")</code>
9. @deprecated
当你需要优雅地逐步淘汰旧函数时,@deprecated
装饰器可以通过在使用已弃用的函数时发出警告来提供帮助。
<code>import warnings def deprecated(func): def wrapper(*args, **kwargs): warnings.warn(f"Function {func.__name__} is deprecated.", category=DeprecationWarning) return func(*args, **kwargs) return wrapper @deprecated def old_function(): return "This is an old function"</code>
10. @memoize
记忆化是一种缓存技术,用于存储昂贵的函数调用的结果以供将来使用。@memoize
装饰器简化了这个过程。
<code>def memoize(func): cache = {} def wrapper(*args): if args in cache: return cache[args] result = func(*args) cache[args] = result return result return wrapper @memoize def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)</code>
这些装饰器可以转换你的 Python 代码,使其更加高效、可读和安全。
暂无评论内容