Google 样式文档字符串示例部分未呈现为代码片段

Cry*_*tal 5 google-style-guide python-sphinx sphinx-napoleon

我最近开始向我的项目添加文档,并且我正在尝试遵循 Google 风格指南。我正在使用 Sphinx 生成文档,并使用 Sphinx 扩展拿破仑来弥合 Google 样式指南和 reST 之间的差距。

我在渲染参数和注释时没有问题,但我似乎无法让示例部分渲染代码片段。

class Chicken(object):
      """Animal that lays egg and has feathers

         Note:
             Chickens love to eat feed

         Example:
             chicken.eats(feed)
      """
Run Code Online (Sandbox Code Playgroud)

我还尝试在示例部分使用双冒号。

Example::
Run Code Online (Sandbox Code Playgroud)

Bro*_*own 5

在分Example::节符和文字块之间需要一个双冒号和一个空行。

请参阅拿破仑文档中的示例:

"""Example Google style docstrings.

This module demonstrates documentation as specified by the `Google Python
Style Guide`_. Docstrings may extend over multiple lines. Sections are created
with a section header and a colon followed by a block of indented text.

Example:
    Examples can be given using either the ``Example`` or ``Examples``
    sections. Sections support any reStructuredText formatting, including
    literal blocks::

        $ python example_google.py

Section breaks are created by resuming unindented text. Section breaks
are also implicitly created anytime a new section starts.
"""
Run Code Online (Sandbox Code Playgroud)

所以,在你的例子中,试试这个:

class Chicken(object):
      """Animal that lays egg and has feathers

         Note:
             Chickens love to eat feed

         Example::

             chicken.eats(feed)
      """
Run Code Online (Sandbox Code Playgroud)