简体   繁体   English

function 或 class 在 Sphinx 中的 Python 的交叉参考文档

[英]Cross reference documentation of function or class in Sphinx for Python

I want, within an explanatory documentation, a reference to the documentation of a Python class and, a little later, to its constructor.我想在解释性文档中引用 Python class 的文档,稍后再引用它的构造函数。 So imagine there is a file所以想象有一个文件

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

and my rst file和我的第一个文件

#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

How can I get the reference working: How and where within the Sphinx files do I need to include the Python file and how do I define (in the Python file) and resolve (in the rst file) the references?如何让参考工作:我需要如何以及在 Sphinx 文件中的何处包含 Python 文件以及如何定义(在 Python 文件中)和解析(在第一个文件中)参考? The expected output would be something like this:预期的 output 将是这样的:

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

where as described the brackets <..> include a link to the documentation and the documentation itself appears after the 'and', referenced in the rst file by :ref:`here` and :ref:`there` , respectively.如前所述,括号 <..> 包含文档链接,文档本身出现在“和”之后,在第一个文件中分别由:ref:`here`:ref:`there`

From the example in the question the module ref_constructor.py从问题中的示例模块ref_constructor.py

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

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

Using the reST file ref_constructor.rst take care to choose the adequate roles, in this case :class: and :meth:使用 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.

You can write the cross-references using fully qualified names or a shortened forms depending on context您可以根据上下文使用完全限定名称或缩短的 forms 编写交叉引用

Cross-referencing Python objects 交叉引用 Python 对象

The name enclosed in this markup can include a module name and/or a class name.此标记中包含的名称可以包括模块名称和/或 class 名称。 For example, :py:func:`filter` could refer to a function named filter in the current module, or the built-in function of that name.例如, :py:func:`filter`可以引用当前模块中名为过滤器的 function 或该名称的内置 function。 In contrast, :py:func:`foo.filter` clearly refers to the filter function in the foo module.相比之下, :py:func:`foo.filter`显然是指 foo 模块中的过滤器 function。

The result结果

HTML 生成文档的屏幕截图

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

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