如何在父类文档字符串中引用子类属性?

问题描述 投票:1回答:1

我正在使用:inherited-members:显示父方法。

在为子级生成的文档中,是否可以引用子级?

class Parent:
   """This is a {Child class name} class

   This is in the {Child group attribute} group
   """
   group = ''
   ...

class Child(Parent):
   group = 'TheChild'
   ...

在为孩子生成的文档中,我想看到:

> class Child(Parent)
>   Bases: Parent
>   This is a Child class.
>   It is in the TheChild group

狮身人面像可能吗?

python parent-child python-sphinx docstring cross-reference
1个回答
0
投票

您可以在文档字符串中使用Sphinx Cross-referencing syntax,并从roles中使用适当的Python domain。之后,autodoc directives中的.rst会处理其余的工作。

示例parent_child.py

class Parent:
    """
    This is a :py:class:`Child` class.

    This is in the :py:data:`Child.group` attribute.
    """
    group = 'TheParent'


class Child(Parent):
    group = 'TheChild'

parent_child.rst

Parent\_Child
=============

.. autoclass:: parent_child.Parent
    :members:
    :undoc-members:
    :show-inheritance:

.. autoclass:: parent_child.Child
    :members:
    :undoc-members:
    :show-inheritance:

结果:

parent_child_image

© www.soinside.com 2019 - 2024. All rights reserved.