2013-04-09 33 views
12

具体的特殊方法,我有一堆它们使用“特殊方法”类:包括在狮身人面像

class Foo(object): 
    "Foo docstring" 

    attr1 = "Attribute!" #: first attribute 
    attr2 = "Another Attribute!" #: second attribute 

    def __init__(self): 
     self.x = 12 

    def say_hello(self): 
     """ 
     say_hello(self) -> None 

     Issue a friendly greeting. 
     """ 
     print "Hello! x is {0}".format(self.x) 

    def __contains__(self,other): 
     """Implement ``other in self``""" 
     return other == self.x 

现在我想生成该使用狮身人面像和车博士HTML文档。我如何告诉狮身人面像文件__contains__?我尝试添加

autodoc_default_flags = ['members', 'undoc-members', 'special-members'] 

conf.py,但也包括__dict__这是我绝对不想。

目前,myproject.rst文件模样的相关部分:

.. automodule:: myproject.foomodule 
    :members: 
    :undoc-members: 
    :show-inheritance: 

编辑加入

.. automodule:: myproject.foomodule 
    :members: 
    :undoc-members: 
    :show-inheritance: 

.. automethod:: myproject.foomodule.Foo.__contains__ 

确实增加了该方法的文档,但在一个单独的部分 - 不为部分Foo类文档。

+0

OTTOMH - 我相信你必须要明确,使用'.. automethod :: __contains__',因为我不相信'特殊members'接受任何形式的滤波参数 – 2013-04-09 13:46:29

+0

问题中的示例并不完全显示记录此方法的引人注目的情况。如果它只是告诉你一个对象是否是一个集合的成员,那么记录'__contains__'没有意义。这是[已经在Python中记录](http://docs.python.org/3/reference/datamodel.html#object.__contains__)。您可能会在文档字符串中提到支持'in'运算符的类。 – 2013-04-09 13:55:22

+0

@JonClements - 看起来很接近。我在上面的'..automethod'东西之后添加了一个'.. automethod :: myproject.foomodule.Foo .__ contains__'',它添加了文档,但是它与文档分开记录。 – mgilson 2013-04-09 13:55:59

回答

3

我目前对此解决方案并非百分百激动,所以我希望有人能够一起改进它。不过,我已经解决了这个问题的办法是做到以下几点:

.. automodule:: myproject.foomodule 
    :members: 
    :undoc-members: 
    :show-inheritance: 

    .. autoclass:: myproject.foomodule.Foo 
     :exclude-members: attr1,attr2 

     .. autoattribute:: myproject.foomodule.Foo.attr1 

     .. autoattribute:: myproject.foomodule.Foo.attr2 

     .. automethod:: myproject.foomodule.Foo.__contains__ 

在这里,我真的需要告诉autodoc避免(自动)记录类属性,然后我需要将它们添加回明确。原因是显然当你明确地嵌套命令,explicit ones come first。如果我只明确地说要添加__contains__,那么它显示在我不喜欢的属性之前。

+0

http://stackoverflow.com/a/21449475/832230有帮助吗? – 2014-01-30 06:42:24

11

您可以添加:

:special-members: 
:exclude-members: __dict__,__weakref__ 

.rst文件,以示特殊的成员,除了__dict____weakref__

7

什么工作对我来说是增加了” .. automethod :: 方法名

指令在类的文档字符串中,而不是在.rst文件中执行。

所以,你可以改变“富文档字符串”到

""" 
Foo docstring 

.. automethod:: __contains__ 
""" 
+0

很高兴知道。我(不幸)不再能够使用狮身人面像 - 所以我无法测试这一点。如果我有机会,我一定会放弃它。 – mgilson 2014-02-28 06:05:02

4

special-members选项现在接受参数(这是狮身人面像1.2的新特性)。

所以这应该工作:

.. automodule:: myproject.foomodule 
    :members: 
    :undoc-members: 
    :special-members: __contains__ 
    :show-inheritance: