具體的特殊方法,我有一堆它們使用「特殊方法」類:包括在獅身人面像
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