如何用斯芬克斯自动doc装饰方法?

2024-09-24 00:33:55 发布

您现在位置:Python中文网/ 问答频道 /正文

当我有一个闭包定义如下的decorator时:

def Decorator(arg):
    class InnerDecorator:
        """Here i use the arg: {arg}"""
        __doc__ = __doc__.format(arg=arg)

        def __init__(self, func):
            self.func = func
            # make arg an instance attribute
            self.arg = arg  

        def __call__(self):
            return self.func()

    return InnerDecorator

我是这样用的:

^{pr2}$

使用交互式shell,我可以看到'foo'和'bar的正确doc字符串:

>>> help(MyClass)

我的问题是: 有没有办法用sphinx为“foo”和“bar”方法生成autodoc? 我试过了

.. autoclass:: MyClass
    :members:

但这行不通。在

目前为止Thx


Tags: selfdocreturnhere定义foodefmyclass
1条回答
网友
1楼 · 发布于 2024-09-24 00:33:55

这个答案帮助我成功了:https://stackoverflow.com/a/15693082/1901330

作为对示例代码的回答,我执行以下操作:

.. autoclass:: module.MyClass

    .. automethod:: module.MyClass.foo(self)
    .. automethod:: module.MyClass.bar(self)

相关问题 更多 >