【问题标题】:Basic Syntax of comments and docstrings in Python codePython 代码中注释和文档字符串的基本语法
【发布时间】:2020-06-24 13:04:29
【问题描述】:

我正在学习如何使用 Sphinx 为我的代码创建文档。在我看到一些这样的例子之后:

def complex(real=0.0, imag=0.0):
    """Form a complex number.

    Keyword arguments:
    real -- the real part (default 0.0)
    imag -- the imaginary part (default 0.0)

    """
    if imag == 0.0 and real == 0.0: return complex_zero
    ...

cmets 使用什么语言让 Sphinx 理解并捕捉它们? 如果没有这种语法和逻辑,Sphinx 在我的代码中看不到 cmets,当我生成 HTML 时,模块是空的。

这里是一个例子:

【问题讨论】:

    标签: python-sphinx docstring sphinx-napoleon


    【解决方案1】:

    您必须区分 cmetsdocstrings(全称为“文档字符串”)。

    参见PEP8 and notice docstrings apply only to 模块、函数、类和方法。对于提到的对象,您可以应用 your_object.__doc__ 以编程方式检索文档字符串;变量没有文档字符串

    关于你的例子:

    def complex(real=0.0, imag=0.0):
        """Form a complex number.
    
        Keyword arguments:
        real -- the real part (default 0.0)
        imag -- the imaginary part (default 0.0)
    
        """
    

    Sphinx 将检索的文档字符串中可以使用 3 种流行的语法:

    1. reST 是基本语法。
    2. Numpy 样式的文档字符串。
    3. Google 风格的文档字符串。

    标准是在 NumpyGoogle 风格 之间进行选择(目前它们提供更好的可读性和功能,以及风格指南)。要使这些工作您需要使用Sphinx-Napoleon extension,请查看官方文档中的概述。

    【讨论】:

    • 非常好!我将开始研究 Numpy 风格以尝试在我的代码中应用。需要任何导入才能使用这种 Numpy 样式吗?还是狮身人面像会自动理解?
    • @HenriqueOliveiraCosta 就像我在帖子中所说,您需要使用 pip 安装 Napoleon 扩展并将其包含在您的 conf.py 中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-18
    • 2010-12-09
    • 1970-01-01
    • 2017-11-26
    • 1970-01-01
    • 2013-04-07
    • 1970-01-01
    相关资源
    最近更新 更多