繁体   English   中英

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

[英]Python Sphinx: How to embed code into a docstring?

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

""" This is a module documentation

Use this module like this:

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

"""

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

""" This is a module documentation

Use this module like this:

.. code-block:: python

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

"""

注意指令和代码块之间的空行——它必须在那里才能正确呈现块。

突出显示代码的另一种方法( 请参阅 mzjn 在这篇文章中的评论)是在代码前的行以两个(!)冒号结尾:

""" This is a module documentation

Use this module like this::

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

"""

::可以解决问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM