Is there any way to automatically 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 * 2 self.var2 = par2 * 2 def method(self): pass
Instance attributes are what we've seen before. These are the attributes that are independent to each object, like the door color or the height, in the previous example. In this example, our Dog class has two instance attributes: . name and .
Class attributes are the variables defined directly in the class that are shared by all objects of the class. Instance attributes are attributes or properties attached to an instance of a class. Instance attributes are defined in the constructor.
Instance attributes are owned by the specific instance of the class, meaning that these attributes can vary from instance to instance of a specific class. On the other hand, Class attributes are owned by the class itself, so the attribute has the same value for each instance of a particular class.
ivar is an "instance variable", or a variable that is set on an instance object (an instance of a class). Typically these would be defined (in Python) inside of an __init__ method.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With