2009-10-05 152 views
8

我一直在努力做到以下幾點:字符串處理文檔字符串

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

我想在這裏完成是有作爲的方法simulate的文檔這種私有方法相同的文檔,除非簡短的介紹。這樣可以避免複製粘貼,保留較短的文件,而不必每次都更新兩個函數的文檔。

但它不起作用。有誰知道一個原因,或者是否有解決方案?

回答

9

更好的解決方案可能是使用一個裝飾,如:

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" 

這樣,你可以做文檔字符串的任意操作。

+0

任意的操作意味着你可以在文檔字符串中看到「爲DOCUM> foo <」,並讓裝飾器使用re替換爲foo .__ doc__,例如。 – 2009-10-06 01:56:31

1

我覺得this section使得它非常清楚:

什麼是文檔字符串?

文檔字符串文字的字符串 發生如在 模塊,函數,類,或方法 定義的第一個語句。這樣的文檔字符串變爲 對象的特殊屬性doc

所以,它不是評價爲一個字符串表達式,它是一個字符串字面