【发布时间】:2021-10-24 07:15:58
【问题描述】:
我希望按名称(使用装饰器)将函数的文档字符串复制到同一文件中。
我可以很容易地使用当前模块之外的函数来做到这一点,但是当涉及到同一个模块(或者更具体地说是同一个类)时我有点困惑
这是我目前所拥有的:
import inspect
def copy_doc(func_name: str):
def wrapper(func):
doc = ... # get doc from function that has the name as func_name
func.__doc__ = doc
return func
retun wrapper
我正在寻找可以做以下两个例子的东西:
例 1:
def this() -> None:
"""Fun doc string"""
return
@copy_doc('this')
def that() -> None:
return
print(that.__doc__)
例 2:
class This:
def foo(self) -> None:
"""Fun doc string"""
return None
@copy_doc('foo')
def bar(self) -> None:
return None
print(This().bar.__doc__)
有什么有趣的想法吗?
【问题讨论】:
-
您可以将@copy_doc('this') 替换为@copy_doc(this) 并尝试func_name.__doc__
-
没错,是的。仍然需要涵盖第二个示例,这是重要的一个:)