如何使用sphinx和autodoc记录“子类”?

时间:2014-03-11 12:07:48

标签: python python-sphinx

假设我有一个带有以下内容的文件example.py:

class Body(object):
    """Representation of a geometric body."""
    def __init__(self):
        self.surface = Surface(self)

class Surface(object):
    """Representation of a geometric surface."""
    def __init__(self, body):
        self.body = body

    def get_surface_area(self):
        """ Calculate surface area of the body """
        print 4

mybody = Body()
mybody.surface.get_surface_area()

当我这样做时

.. automodule:: example
    :members:

我得到了所有两个类并记录了函数。但是,我如何指出我班级的预期用途,即mybody.surface.get_surface_area(),并且还有正确的链接?

1 个答案:

答案 0 :(得分:1)

我不完全确定这是否是您想要的,但这里有关于如何记录您的课程的建议:

class Body(object):
    """Representation of a geometric body.

      :ivar surface: a :class:`example.Surface` instance 

      Usage example:

      >>> mybody = Body()
      >>> mybody.surface.get_surface_area()
      >>> 4

      Another way of doing it:

      >>> mybody = Body()
      >>> mysurface = Surface(mybody)
      >>> mysurface.get_surface_area()
      >>> 4

    """
    def __init__(self):
        self.surface = Surface(self)  

class Surface(object):
    """Representation of a geometric surface.

     :ivar body: The supplied :class:`example.Body` instance

    """
    def __init__(self, body):
        self.body = body             

    def get_surface_area(self):
        """ Calculate surface area of the body """
        print 4