2012-04-04 39 views
0

屬性通過如何文檔類與NaturalDocs

""" 
    Function: myfunc 
    Parameters: 
    a - First parameter 
    b - First parameter 
""" 

我可以記錄一個函數,它就會在類摘要中列出。 我該如何做與屬性類似的東西? 因爲我沒有在Python申報他們,我希望像

""" ---------------------------------------------------------------------------- 
    Attributes: 
    first - First attribute of the class 
    second - Second one 
""" 

即不工作...

回答

1

如果你不聲明類顯式的屬性 - 我以爲你只是爲它們分配在構造函數中值 - 你可以把自己的意見與類註釋起來:

""" 
    Class: MyClass 
    Describe the class here. 

    Attributes: 
    attr1 - First attribute of the class 
    attr2 - Second one 
""" 
class MyClass: 

    def __init__(self, arg1): 
     self.attr1 = arg1 
     self.attr2 = "attr2" 

您可以爲方法做同樣的太。這是最簡單的方法,但是您不會在索引中分別列出類別成員,這是一個巨大的缺點。如果你提供了一個前綴的文件中每類成員的引用將工作:

""" 
    Class: MyClass 
    Describe the class here. 

    Attribute: attr1 
    First attribute of the class 

    Attribute: attr2 
    Second one 
""" 
class MyClass: 

    # Constructor: __init__ 
    # Describe the constructor. 
    # 
    # Parameters: 
    # arg1 - The first argument. 
    def __init__(self, arg1): 
     self.attr1 = arg1 
     self.attr2 = "attr2" 

    # Method: method1 
    # Describe the method here. 
    def method1(self): 
     print("method1") 

加前綴的評論是不是在評論剛剛實施前通常放反正方法有問題。如果你沒有明確地聲明你的屬性來爲自己的評論留下自然的位置,它會使課堂評論雜亂無章。您也可以將評論分成更多部分。注意你可以混合行和塊註釋。

兩個備註:如果您想使用"""分隔塊註釋,而不是僅僅通過#前綴行註釋,你必須將下列行添加到Languages.txt在NaturalDocs項目目錄:顯然

Alter Language: Python 

    Block Comment: """ """ 

你例如關鍵字Attribute而不是Property,默認情況下它由NaturalDocs識別。以下內容添加到Topics.txt在NaturalDocs項目目錄有它承認過:

Alter Topic Type: Property 

    Add Keywords: 
     attribute, attributes 

--- Ferda

0

檢查文檔字符串這裏所描述http://epydoc.sourceforge.net/manual-docstring.html

變量也可以使用評論文檔進行記錄。如果變量賦值立即以特殊標記「#:」開頭的註釋開頭,或者由同一行註釋,則它被視爲該變量的文檔字符串:

#: docstring for x 
x = 22 
x = 22 #: docstring for x 
+0

似乎並沒有得以順利。我通常只在__init__函數內聲明類屬性。 – HWende 2012-04-04 11:36:28

+1

我使用的是NaturalDocs,python有基本支持:**僅限顯式文檔。只有您編寫Natural Docs文檔的內容纔會顯示在輸出中。** – HWende 2012-04-04 11:38:19