如何在Sphinx中的方法内部自动记录功能 [英] How to autodoc a function inside a method in Sphinx
问题描述
代码示例:
A类(对象):
def do_something(个体):
doc_a
def inside_function():
doc_b
通过
通过
我尝试过:
.. autoclass :: A
.. autofunction :: A.do_something.inside_function
但它不起作用。 / p>
是否可以通过某种方式为我生成 doc_b
?
一个函数中的一个函数在局部变量作用域内。无法从函数外部访问函数的局部变量:
>> def x():
... def y():
...通过
...
>> x
<函数x在0x7f68560295f0>
>> x.y
追溯(最近一次通话最后一次):
文件< stdin>,在< module>中的第1行。
AttributeError:'函数'对象没有属性'y'
如果Sphinx无法
一种可行的解决方法是将函数分配给函数的变量,如下所示: / p>
>> def x():
... def _y():
... pass
... xy = _y
...
一开始将无法访问:
>>> x.y
追溯(最近一次通话最后一次):
文件< stdin>,在< module>中的第1行。
AttributeError:函数对象没有属性 y
但在第一次调用后
>> x()
>> x.y
<函数_y at 0x1a720c8>
如果在Sphinx导入模块时函数被执行,这可能会起作用。
Code example:
class A(object):
def do_something(self):
""" doc_a """
def inside_function():
""" doc_b """
pass
pass
I tried:
.. autoclass:: A
.. autofunction:: A.do_something.inside_function
but it doesn't work.
Is there some way to generate doc_b
for me?
A function within a function is at the local variable scope. The local variables of a function are not accessible from outside of the function:
>>> def x():
... def y():
... pass
...
>>> x
<function x at 0x7f68560295f0>
>>> x.y
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'function' object has no attribute 'y'
If Sphinx can't get a reference to the function, it can't document it.
A workaround that might work is to assign the function to a variable of the function, like this:
>>> def x():
... def _y():
... pass
... x.y = _y
...
It won't be accessible at first:
>>> x.y
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'function' object has no attribute 'y'
But after the first invocation of the function, it will be:
>>> x()
>>> x.y
<function _y at 0x1a720c8>
This might work if the function gets executed when Sphinx imports the module.
这篇关于如何在Sphinx中的方法内部自动记录功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!