问题
I want to document some classes which all derive from the same base class with some common attributes and I would like to repeat the documentation for every attribute in the subclasses, so that I can see all the attributes for a class in a single place.
So for instance I have this code:
class Base(object):
"""Base class."""
#: First attribute
a = int
#: Second attribute
b = str
class FirstChild(Base):
"""First Child of Base."""
#: Child attribute
c = float
class SecondChild(Base):
"""Second Child of Base."""
pass
and I have this rst:
.. automodule:: example
:members:
:show-inheritance:
The output will be like this:
class class example.Base
Bases: "object"
Base class.
a
First attribute
alias of "int"
b
Second attribute
alias of "str"
class class example.FirstChild
Bases: "example.Base"
First Child of Base.
c
Child attribute
alias of "float"
class class example.SecondChild
Bases: "example.Base"
Second Child of Base.
Is there a way to generate documentation such that the child classes will also have the inherited attributes?
For instance:
class class example.FirstChild
Bases: "example.Base"
First Child of Base.
a
First attribute
alias of "int"
b
Second attribute
alias of "str"
c
Child attribute
alias of "float"
class class example.SecondChild
Bases: "example.Base"
Second Child of Base.
a
First attribute
alias of "int"
b
Second attribute
alias of "str"
回答1:
You need to add the :inherited-members:
option, quote from the docs:
For classes and exceptions, members inherited from base classes will be left out when documenting all members, unless you give the inherited-members flag option, in addition to members.
来源:https://stackoverflow.com/questions/30344289/how-can-i-show-inherited-members-of-a-class-in-my-sphinx-documentation