Sphinx for Python中函数或类的交叉引用文档

2024-06-07 09:28:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在一个解释性文档中,引用一个Python类的文档,稍后再引用它的构造函数。假设有一个文件

# MyLib/MyClass.py
class MyClass:
    """Class Introduction"""
    def __init__(paramX):
        """:param paramX: Xes out things"""

还有我的rst文件

#MyLib/docs/source/MyDoc.rst
Some text where :ref:`here` is my reference to "Class Introduction"
 and :ref:`there` follows my reference to the __init__ method documentation

如何使引用正常工作:Sphinx文件中需要如何和在何处包含Python文件,以及如何定义(在Python文件中)和解析(在rst文件中)引用?预期输出如下所示:

Some text where <link to documentation of class _MyClass_>...
 and 
    'class MyClass
        def __init__(paramX):
           paramX: Xes out things'

如前所述,括号<&燃气轮机;包括指向文档的链接,文档本身出现在rst文件中分别由:ref:`here` :ref:`there` 引用的“和”之后


Tags: 文件to文档refinitdefmyclassrst
1条回答
网友
1楼 · 发布于 2024-06-07 09:28:27

根据问题中的示例,模块ref_constructor.py

class MyClass:
    """Class Introduction."""
    def __init__(paramX):
        """The docstring of constructor.

        :param paramX: Xes out things.
        :type paramX: str
        """

使用reST文件ref_constructor.rst注意选择适当的角色,在本例中是:class::meth:

Reference to class constructor
               

.. automodule:: ref_constructor


.. here begins de documentation.

Some text where :class:`ref_constructor.MyClass` is my reference to "Class Introduction"
and :meth:`ref_constructor.MyClass.__init__` follows my reference to the __init__ method documentation.


Some text where :class:`MyClass` is my reference to "Class Introduction"
and :meth:`MyClass.__init__` follows my reference to the __init__ method documentation.

您可以根据上下文使用完全限定名或缩写形式编写交叉引用

Cross-referencing Python objects

The name enclosed in this markup can include a module name and/or a class name. For example, :py:func:`filter` could refer to a function named filter in the current module, or the built-in function of that name. In contrast, :py:func:`foo.filter` clearly refers to the filter function in the foo module.

结果

screenshot of HTML generated documentation

相关问题 更多 >

    热门问题