在狮身人面像中包含特定的特殊方法 [英] Include specific special-methods in sphinx

查看:86
本文介绍了在狮身人面像中包含特定的特殊方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆使用特殊方法的类:

I have a bunch of classes which use "special-methods":

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

现在我想使用Sphinx和autodoc为此生成html文档。如何告诉Sphinx记录 __ contains __ ?我尝试添加

now I would like to generate html documentation for this using Sphinx and autodoc. How do I tell Sphinx to document __contains__? I tried adding

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

conf.py ,但这还包括 __ dict __ 我绝对不想要。

to conf.py, but that also included __dict__ which I definitely don't want.

当前, myproject.rst 文件的相关部分如下:

Currently, the relevant portions of the myproject.rst file look like:

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

编辑添加

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

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

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

does add documentation of that method, but in a separate section -- Not as part of the Foo class documentation.

推荐答案

您可以添加:

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

.rst 文件以显示特殊成员,但 __ dict __ __ weakref __

To the .rst file in order to show special members, except __dict__ and __weakref__

这篇关于在狮身人面像中包含特定的特殊方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆