Python文档字符串中的字符串操作 [英] String manipulation in Python docstrings
本文介绍了Python文档字符串中的字符串操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在尝试执行以下操作:
I've been trying to do the following:
#[...]
def __history_dependent_simulate(self, node, iterations=1,
*args, **kwargs):
"""
For history-dependent simulations only:
""" + self.simulate.__doc___
我在这里尝试完成的是使用与该私有方法相同的文档作为文档方法模拟
,除了简短介绍。这将允许我避免复制粘贴,保留较短的文件,而不必每次更新两个功能的文档。
What I tried to accomplish here is to have the same documentation for this private method as the documentation of the method simulate
, except with a short introduction. This would allow me to avoid copy-pasting, keep a shorter file and not have to update the documentation for two functions every time.
但它不起作用。有没有人知道为什么,还是有解决方案?
But it doesn't work. Does anyone know of a reason why, or whether there is a solution?
推荐答案
更好的解决方案可能是使用装饰器,例如:
A better solution is probably to use a decorator, eg:
def add_docs_for(other_func):
def dec(func):
func.__doc__ = other_func.__doc__ + "\n\n" + func.__doc__
return func
return dec
def foo():
"""documentation for foo"""
pass
@add_docs_for(foo)
def bar():
"""additional notes for bar"""
pass
help(bar) # --> "documentation for foo // additional notes for bar"
这样可以对文档字符串进行任意操作。
That way you can do arbitrary manipulations of docstrings.
这篇关于Python文档字符串中的字符串操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文