gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 20:55:06 25 4
gpt4 key购买 nike

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

#[...]
def __history_dependent_simulate(self, node, iterations=1,
*args, **kwargs):
"""
For history-dependent simulations only:
""" + self.simulate.__doc___

我在这里试图完成的是让这个私有(private)方法的文档与方法 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"

这样你就可以对文档字符串进行任意操作。

关于python - Python 文档字符串中的字符串操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1519029/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com