如何在sphinx doc中显示实例属性? [英] how to show instance attributes in sphinx doc?
本文介绍了如何在sphinx doc中显示实例属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法在sphinx文档中自动显示变量 var1 和 var2 及其初始值?
class myClass():
类
的描述
def __init __(self ,par1,par2):
self.var1 = par1
self.var2 = par2
def method():
pass
解决方案
您的变量是实例变量,而不是类变量。
如果没有附加一个docstring(或$ code>#:doc comment)到这些变量,它们将不会被记录。你可以这样做:
class MyClass(object):
类的描述
def __init __(self,par1,par2):
self.var1 = par1#:初始值:par1
self。 var2 = par2#:初始值:par2
def method(self):
pass
但是我希望通过使用信息字段:
类MyClass(object):
说明对于class
:ivar var1:初始值:par1
:ivar var2:初始值:par2
def __init __(self, par1,par2):
self.var1 = par1
self.var2 = par2
def method(self):
pass
另请参见:
- http://www.sphinx-doc.org/ext/autodoc.html#directive-autoattribute
- 如何使Python / Sphinx文档对象属性仅在__init__中声明?
- autodoc和明确指定的实例属性问题
Is there any way how to automaticaly show variables var1 and var2 and their init-values in sphinx documentation?
class myClass():
"""
Description for class
"""
def __init__(self, par1, par2):
self.var1 = par1
self.var2 = par2
def method():
pass
解决方案
Your variables are instance variables, not class variables.
Without attaching a docstring (or a #:
"doc comment") to the variables, they won't be documented. You could do as follows:
class MyClass(object):
"""
Description for class
"""
def __init__(self, par1, par2):
self.var1 = par1 #: initial value: par1
self.var2 = par2 #: initial value: par2
def method(self):
pass
But I would prefer to include variable documentation by using info fields:
class MyClass(object):
"""
Description for class
:ivar var1: initial value: par1
:ivar var2: initial value: par2
"""
def __init__(self, par1, par2):
self.var1 = par1
self.var2 = par2
def method(self):
pass
See also:
- http://www.sphinx-doc.org/ext/autodoc.html#directive-autoattribute
- How can I make Python/Sphinx document object attributes only declared in __init__?
- Problems with autodoc and explicitly specified instance attributes
这篇关于如何在sphinx doc中显示实例属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文