Python Sphinx:如何将代码嵌入到文档字符串中?

Ale*_*x44 7 python docstring python-3.x python-sphinx

如何将代码嵌入到文档字符串中,以告诉 Sphinx 将代码格式化为与 Markdown 中类似的格式(不同的背景颜色、等宽无字体)?例如记录代码使用示例。

""" This is a module documentation

Use this module like this:

   res = aFunction(something, goes, in)
   print(res.avalue)

"""
Run Code Online (Sandbox Code Playgroud)

Bła*_*lik 12

有几种方法可以做到这一点。我认为在你的情况下最明智的是.. code-block::

""" This is a module documentation

Use this module like this:

.. code-block:: python

   res = aFunction(something, goes, in)
   print(res.avalue)

"""
Run Code Online (Sandbox Code Playgroud)

请注意指令和代码块之间的空行 - 它必须存在才能使块正确呈现。