Python狮身人面像: How to embed code into a docstring?

标签 python python-3.x python-sphinx docstring

如何将代码嵌入到文档字符串中,以告诉 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/

相关文章:

python-sphinx - Sphinx 中的侧栏导航不完整

latex - LaTeX 中的 Sphinx 交叉引用

python - 为什么 Python 不从 Latin Extended-A 写入字符(写入文件时出现 UnicodeEncodeError)?

python - 将字典列表中具有相同值的项目添加到字典中

python-3.x - 将python2 'file'类的一个子类移植到python3

python - 根据 MA​​C 地址和用户名生成确定性 UUID

python - 类 X 的 __new__ 方法中的 X 类型的 Unpickling 对象在返回 unpickled 对象时调用 __init__,为什么?

powershell 中的 python 与 python CLI

python AttributeError assert_called

python - .. 重组文本中的 plot 指令