简体   繁体   English

如何使用 sphinx 自动记录装饰方法?

[英]How to autodoc decorated methods with sphinx?

When i have a decorator with a closure defined like this:当我有一个带有如下定义的闭包的装饰器时:

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

Which I use like this:我像这样使用:

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

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

I can see the correct doc-strings for 'foo' and 'bar using the interactive-shell with:我可以使用交互式 shell 看到 'foo' 和 'bar 的正确文档字符串:

>>> help(MyClass)

My question is: Is there a way to generate autodoc for methods 'foo' and 'bar' with sphinx?我的问题是:有没有办法用 sphinx 为方法 'foo' 和 'bar' 生成自动文档? I tried,我试过,

.. autoclass:: MyClass
    :members:

but that does not work.但这不起作用。

Thx so far到目前为止,谢谢

This answer helped me to get it work: https://stackoverflow.com/a/15693082/1901330这个答案帮助我让它工作: https : //stackoverflow.com/a/15693082/1901330

As answer to my example code i do following:作为对我的示例代码的回答,我执行以下操作:

.. autoclass:: module.MyClass

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM