【问题标题】:How to autodoc decorated methods with sphinx?如何使用 sphinx 对装饰方法进行 autodoc?
【发布时间】:2017-06-23 15:23:29
【问题描述】:

当我有一个带有这样定义的闭包的装饰器时:

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

我是这样使用的:

class MyClass(object):
    @Decorator("ARG")
    def foo(self):
        pass

    @Decorator("Other ARG")
    def bar(self):
        pass

我可以使用交互式外壳查看“foo”和“bar”的正确文档字符串:

>>> help(MyClass)

我的问题是: 有没有办法用 sphinx 为方法 'foo' 和 'bar' 生成自动文档? 我试过了,

.. autoclass:: MyClass
    :members:

但这不起作用。

谢谢

【问题讨论】:

    标签: python python-sphinx python-decorators


    【解决方案1】:

    这个答案帮助我让它工作:https://stackoverflow.com/a/15693082/1901330

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

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

    【讨论】:

      猜你喜欢
      • 2018-10-20
      • 2011-04-10
      • 1970-01-01
      • 2011-08-01
      • 1970-01-01
      • 2020-09-25
      • 2017-07-12
      • 2015-02-04
      • 2017-09-18
      相关资源
      最近更新 更多