使用functools.wraps可保留被装饰函数的元信息,避免函数名、文档字符串等丢失。1. 不使用wraps时,装饰器会掩盖原函数的__name__和__doc__;2. 使用@wraps(func)后,wrapper继承原函数的元数据,确保调试和文档正确;3. 适用于日志、计时、权限等通用装饰器,提升代码可维护性。

functools.wraps 是 Python 中用于装饰器开发的一个实用工具,它的主要作用是保留被装饰函数的元信息(如函数名、文档字符串、参数列表等)。如果不使用 wraps,装饰器会“掩盖”原函数的这些信息,给调试和文档生成带来困扰。
为什么需要 wraps?
考虑一个简单的装饰器:def my_decorator(func):
def wrapper(*args, **kwargs):
print("调用前")
result = func(*args, **kwargs)
print("调用后")
return result
return wrapper
<p>@my_decorator
def say_hello():
"""打招呼函数"""
print("Hello!")</p><p>print(say_hello.<strong>name</strong>) # 输出:wrapper
print(say_hello.<strong>doc</strong>) # 输出:None</p>
可以看到,函数名变成了 wrapper,文档字符串丢失了。这在实际开发中不利于调试和框架识别。使用 wraps 保留原函数信息
通过 functools.wraps 可以解决这个问题:from functools import wraps
<p>def my_decorator(func):
@wraps(func)
def wrapper(*args, *<em>kwargs):
print("调用前")
result = func(</em>args, **kwargs)
print("调用后")
return result
return wrapper</p><p>@my_decorator
def say_hello():
"""打招呼函数"""
print("Hello!")</p><p>print(say_hello.<strong>name</strong>) # 输出:say_hello
print(say_hello.<strong>doc</strong>) # 输出:打招呼函数</p>
加上 @wraps(func) 后,wrapper 函数会复制 func 的名称、文档字符串、模块名等元数据,使装饰后的函数看起来更“像”原函数。实际应用场景
wraps 在编写通用装饰器时非常有用,比如:- 日志记录装饰器
- 性能计时装饰器
- 权限校验装饰器
- 缓存装饰器
from functools import wraps
import time
<p>def timer(func):
@wraps(func)
def wrapper(*args, *<em>kwargs):
start = time.time()
result = func(</em>args, **kwargs)
end = time.time()
print(f"{func.<strong>name</strong>} 执行耗时: {end - start:.2f}s")
return result
return wrapper</p><p>@timer
def slow_function():
time.sleep(1)</p><p>slow_function() # 输出函数名和耗时</p><div class="aritcle_card flexRow">
<div class="artcardd flexRow">
<a class="aritcle_card_img" href="/ai/2457" title="PathFinder"><img
src="https://img.php.cn/upload/ai_manual/001/246/273/176646000963539.png" alt="PathFinder" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a href="/ai/2457" title="PathFinder">PathFinder</a>
<p>AI驱动的销售漏斗分析工具</p>
</div>
<a href="/ai/2457" title="PathFinder" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span> </a>
</div>
</div>
基本上就这些。只要写装饰器,建议都用 @wraps,避免丢失函数元信息,提升代码可维护性。不复杂但容易忽略。










