例如,我有以下代码:

# Solve for coefficients of quadratic approximation
def quad(p, x):
    """Solves for the coefficients of the quadratic approximation of a
    polynomial ``p`` at points ``x``.

    :param :cls:`numpy.polynomial.Polynomial` p:
        The polynomial to be approximated by a quadratic function.
    :param list x:
        The three points along which the quadratic function is to be fitted.
    """

注意我说的那部分。如何直接链接到:cls:numpy.polynomial.Polynomial类的文档?

最佳答案

您可以使用intersphinx进行此操作。
将这些行添加到conf.py:

extensions = ["sphinx.ext.intersphinx"]        # Or edit existing 'extensions' list
intersphinx_mapping = {'numpy': ('http://docs.scipy.org/doc/numpy/', None)}

在docstring中使用此rest标记:
:param p: The polynomial to be approximated by a quadratic function.
:type p: :class:`~numpy:numpy.polynomial.polynomial.Polynomial`

这将导致从quad()函数文档到numpy.polynomial.polynomial.Polynomial文档的超链接(文本为“多项式”)。
numpy.polynomial.Polynomialnumpy.polynomial.polynomial.Polynomial可以互换使用(参见http://docs.scipy.org/doc/numpy/reference/routines.polynomials.classes.html#basics)。后一种形式是参考文档中显示的形式,可以作为interphinx目标使用。
如果希望链接文本是完全限定的类名,请删除tilde(~)字符。有关“信息字段列表”和对python对象的交叉引用的更多信息,请参见http://sphinx-doc.org/domains.html

07-24 09:44
查看更多