python - literalinclude : how to include only a variable using pyobject?

标签 python python-sphinx restructuredtext

我正在使用 Sphinx 来记录我的部分 Python 代码。

我只想在我的文档中包含我的类中的一个变量。

例如,我试过这个但没有成功:

.. literalinclude:: MyClass.py
    :pyobject: MyClass.aVariable
    :language: python

我只看到一个解决方案,但如果我的代码在下一个版本中更改,变量的行号可能会更改:

.. literalinclude:: MyClass.py
    :language: python
    :lines: 2-3

是否有另一种解决方案来仅过滤此变量?

最佳答案

作为解决方法,我使用了 start-after 和 end-before 选项,但我必须在代码中的变量前后添加两条注释。

例如:

类(class):

class MyClass():
    # Start variables
    aVariable = {"a":123 , "b":456}
    # End variables 
        def __init__(self):
            pass

RST 文件:

.. literalinclude:: MyClass.py
    :start-after: # Start variables 
    :end-before: # End variables 
    :language: python

它并不完美,但它确实有效。

关于python - literalinclude : how to include only a variable using pyobject?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31561895/

相关文章:

restructuredtext - 在reStructuredText中,如何同时使用强调和上标?

python - 使用 Python 预处理机器学习中的原始数据

python - pdflatex 在大量数字后挂起

python - 在 Sphinx 中创建 LaTeX 数学宏

stylesheet - 重构文本样式

html-lists - "Unexpected indentation"带有重组文本列表

python - 列表数组的映射和过滤功能

python - 如何在过滤器中添加if语句?

python - Keras 中的 LSTM 和 fit_generator 错误 "You must compile your model before using it"

python-sphinx - 警告 : document isn't included in any toctree for included file