Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python文档字符串中的字符串操作_Python_String_Documentation - Fatal编程技术网

Python文档字符串中的字符串操作

Python文档字符串中的字符串操作,python,string,documentation,Python,String,Documentation,我一直在努力做到以下几点: #[...] def __history_dependent_simulate(self, node, iterations=1, *args, **kwargs): """ For history-dependent simulations only: """ + self.simulate.__doc___ 我在这里试图实现的是,除了

我一直在努力做到以下几点:

#[...]
    def __history_dependent_simulate(self, node, iterations=1,
                                     *args, **kwargs):
        """
        For history-dependent simulations only:
        """ + self.simulate.__doc___
我在这里试图实现的是,除了简短的介绍之外,这个私有方法的文档与方法
simulate
的文档是相同的。这将允许我避免复制粘贴,保留较短的文件,并且不必每次更新两个函数的文档


但它不起作用。有人知道原因吗,或者是否有解决方案吗?

更好的解决方案可能是使用装饰器,例如:

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"
这样,您就可以对docstring进行任意操作。

我认为这一点非常清楚:

什么是文档字符串?

docstring是一个字符串文本,它 作为中的第一个语句发生 模块、函数、类或方法 定义。这样的docstring就变成了 doc的特殊属性 反对


因此,它不是一个计算为字符串的表达式,而是一个字符串文字。

任意操作意味着您可以使用“SEE DOCS FOR>foo”