使用Sphinx的autodoc对属性进行文档字符串继承 [英] Docstring inheritance for properties using sphinx's autodoc

查看:87
本文介绍了使用Sphinx的autodoc对属性进行文档字符串继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的类:

class MyBase(object):
   x = 3
   """Documentation for property x"""

以及另一个继承它的类:

and another class that inherits it:

class MyObj(MyBase):
   x = 0

当我使用sphinx的自动文档生成文档时,没有记录 MyObj.x 。有什么方法可以从 MyBase.x 继承文档字符串吗?我发现 DocInherit ,但是由于它使用装饰器,因此仅适用于类方法。可以使用属性来执行此操作吗?

When I use sphinx's autodoc to generate documentation, MyObj.x is not documented. Is there any way to inherit the docstring from MyBase.x? I found DocInherit but since this uses a decorator, it only works for class methods. Any way to do this with properties?

推荐答案

我找到了使用属性函数的解决方法:

I found a workaround using the property function:

class MyBase(object):
   _x = 3
   x = property( lambda s: s._x, doc="Documentation for property x")

class MyObj(MyBase):
   _x = 0

在给定实例变量的情况下,这很不错:

This is nice in that given an instance variable:

>>> m = MyObj()
>>> m.x
0

一个人可以致电 help(m)并获得属性 x 的正确文档,并且狮身人面像也可以正确识别此属性。

one can call help(m) and get proper documentation of property x and sphinx also picks this up correctly.

这篇关于使用Sphinx的autodoc对属性进行文档字符串继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆