具体的特殊方法,我有一堆它们使用“特殊方法”类:包括在狮身人面像
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
类文档。
OTTOMH - 我相信你必须要明确,使用'.. automethod :: __contains__',因为我不相信'特殊members'接受任何形式的滤波参数 – 2013-04-09 13:46:29
问题中的示例并不完全显示记录此方法的引人注目的情况。如果它只是告诉你一个对象是否是一个集合的成员,那么记录'__contains__'没有意义。这是[已经在Python中记录](http://docs.python.org/3/reference/datamodel.html#object.__contains__)。您可能会在文档字符串中提到支持'in'运算符的类。 – 2013-04-09 13:55:22
@JonClements - 看起来很接近。我在上面的'..automethod'东西之后添加了一个'.. automethod :: myproject.foomodule.Foo .__ contains__'',它添加了文档,但是它与文档分开记录。 – mgilson 2013-04-09 13:55:59