Including docstring in Sphinx Documentation

℡╲_俬逩灬. 提交于 2019-12-03 06:38:03

After looking through the source and experimenting - here is how to do it in Sphinx 1.1.

In your conf.py file create a new MethodDocumenter subclass. Here you can set a new "objtype", make sure the docstring is not indented, and remove the title.

from sphinx.ext import autodoc

class SimpleDocumenter(autodoc.MethodDocumenter):
    objtype = "simple"

    #do not indent the content
    content_indent = ""

    #do not add a header to the docstring
    def add_directive_header(self, sig):
        pass

Then make sure this is added to the available documenters with the following function (again in conf.py):

def setup(app):
    app.add_autodocumenter(SimpleDocumenter)

Then when you just want to display a method's docstring use the following format in your .txt or .rst files. Just prefix your objname with auto.

.. autosimple:: mod.MyClass.my_method
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!