微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

如何将代码嵌入到文档字符串中,以告诉Sphinx像使用Markdown(不同的背景颜色,等距的sans字体)一样格式化代码?例如,记录一个代码用法示例。

""" 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)

"""

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

,

突出显示代码的另一种方法(see the comment of mzjn on this post)是在代码之前的行末以两个(!)冒号结尾:

""" This is a module documentation

Use this module like this::

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

"""

::可以解决问题。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。