![](/img/trans.png)
[英]How to indicate a valid range in a Python docstring using 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.