Python 未显示docstring中继承的属性
我试图用Sphinx来记录一个基类和两个带有google风格docstring类的子类。我对遗传属性特别不适应:Python 未显示docstring中继承的属性,python,python-sphinx,autodoc,Python,Python Sphinx,Autodoc,我试图用Sphinx来记录一个基类和两个带有google风格docstring类的子类。我对遗传属性特别不适应: class Base(object): """Base class. Attributes: a (int): one attribute b (int): another one """ def __init__(self): self.a = 3 self.b = 5 cla
class Base(object):
"""Base class.
Attributes:
a (int): one attribute
b (int): another one
"""
def __init__(self):
self.a = 3
self.b = 5
class FirstChild(Base):
"""First Child of Base.
Attributes:
c (float): child class attribute
"""
def __init__(self):
self.c = 3.1
class SecondChild(Base):
"""Second Child of Base."""
pass
以下是rst文件:
.. automodule:: my_package.my_module
:members:
:undoc-members:
:show-inheritance:
:inherited-members:
Sphinx仅在类基础上显示属性a和b。在FirstChild中,只有c,而在SecondChild中并没有属性,即使带有:inherited members:
标记
有没有一种方法可以在子类中显示a、b和c,而不必在FirstChild/SecondChild文档字符串中复制/粘贴描述
谢谢 通过使用decorator,您可以从父类中提取属性并将它们插入继承的类中
def docstring_inherit(父项):
def继承(obj):
空格=“”
如果不是str(对象文档)。包含属性:
对象。\uuuu文档\uuuu+=“\n”+空格+”属性:\n
obj.\uuuuuuu doc\uuuuuuu=str(obj.\uuuuuuuu doc\uuuuuuu).rstrip()+“\n”
对于父项中的属性。_udoc_uu.split(“属性:\n”)[-1].lstrip().split(“\n”):
obj.\uuuuu doc\uuuuuu+=空格*2+str(属性).lstrip().rstrip()+“\n”
返回obj
回归继承
类基(对象):
“”基类。
属性:
a(int):一个属性
b(int):另一个
"""
定义初始化(自):
self.a=3
self.b=5
@docstring_inherit(基本)
第一个孩子(基本):
“基地的第一个孩子。
属性:
c(float):子类属性
"""
定义初始化(自):
self.c=3.1
我希望它能解决其他人同样怀疑的问题
打印(第一个子文件)
"""
基地的第一个孩子。
属性:
c(float):子类属性
a(int):一个属性
b(int):另一个
"""
此未解决问题与您的问题类似: