2012-04-04 54 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