如何将代码嵌入到文档字符串中,以告诉 Sphinx 格式化代码,就像在 Markdown 中所做的一样(不同的背景颜色,等宽的无字体)?例如记录一个代码使用示例。
""" This is a module documentation
Use this module like this:
res = aFunction(something, goes, in)
print(res.avalue)
"""
最佳答案
There are a few ways to do it .我认为在您的情况下最明智的是 .. code-block::
""" This is a module documentation
Use this module like this:
.. code-block:: python
res = aFunction(something, goes, in)
print(res.avalue)
"""
请注意指令和代码块之间的空白行 - 它必须存在以便 block 正确呈现。
关于Python狮身人面像: How to embed code into a docstring?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64451966/