How to override constructor parameters in Sphinx w

2020-03-20 02:22发布

Let's say I have a class like this:

class MyClass(object):
    """ Summary docs for my class.

    Extended documentation for my class.
    """

    def __init__(self, *args):
        self.values = np.asarray(args)

If I use Sphinx with the autodoc extension to document this class like so:

.. automodule:: mymodule
   :members:

...the constructor signature appears as MyClass(*args). I would rather override this and document it as, say, MyClass(first, second, third).

If this were a function, I could override the signature in the first line of the docstring. But that trick doesn't seem to work on a class docstring. So how can I override the constructor signature?

1条回答
我命由我不由天
2楼-- · 2020-03-20 02:42

I think that the best option for you is to do something like this:

.. automodule:: mymodule
    :members:
    :exclude-members: MyClass

    .. autoclass:: MyClass(first, second, third)

MyClass will have params overwritten and other members of mymodule will be autodocumented. You need to exclude MyClass using :exclude-members: because it will be included twice. I think it's the simplest solution at the moment.

查看更多
登录 后发表回答