简体   繁体   English

狮身人面像中的方法组的文档字符串

[英]Docstring for groups of methods in sphinx

Is it possible to add docstrings for groups of methods in the Sphinx generated documentation? 是否可以在Sphinx生成的文档中为方法组添加文档字符串?

For example, I would like to have something like: 例如,我想要类似的东西:

class MyClass():
    """Doc of the class"""
    def __init__(self):
        pass

    """----- The following part is about imports -----"""

    def import_from_source_1(self):
        """Doc of import_from_source_1"""
        pass

    def import_from_source_2(self):
        """Doc of import_from_source_2"""
        pass

    """----- The following part is about exports-----"""

    def export_to_dest_1(self):
        """Doc of export_to_dest_1"""
        pass

    def export_to_dest_2(self):
        """Doc of export_to_dest_2"""
        pass

And the expected output would be: 预期的输出将是:

MyClass
    Doc of the class

----- The following part is about imports -----
import_from_source_1
    Doc of import_from_source_1

import_from_source_2
    Doc of import_from_source_2

----- The following part is about exports-----
export_to_dest_1
    Doc of export_to_dest_1

export_to_dest_2
    Doc of export_to_dest_2

Note that my goal is not (only) to group methods (as found in Group method docstrings in sphinx ), but to add a docstring to the group. 请注意,我的目标不是(仅)将方法分组(如在sphinx中的“ 分组方法”文档字符串中找到的),而是将文档字符串添加到组中。

A docstring is a string literal that occurs as the first statement in a module, function, class, or method definition ( https://python.org/dev/peps/pep-0257/#id15 ). docstring是一个字符串文字,它作为模块,函数,类或方法定义( https://python.org/dev/peps/pep-0257/#id15 )中的第一条语句出现。 You cannot have "extra" docstrings like the ones in the question. 您不能像问题中那样具有“额外”的文档字符串。

However, you can do the grouping by using automethod : 但是,您可以使用automethod进行分组:

.. currentmodule:: mymodule

.. autoclass:: MyClass

   The following part is about imports

   .. automethod:: import_from_source_1
   .. automethod:: import_from_source_2

   The following part is about exports

   .. automethod:: export_to_dest_1
   .. automethod:: export_to_dest_2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM