如何编写有意义的文档字符串?

发布于 2024-07-14 19:16:49 字数 1429 浏览 13 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

平安喜乐 2024-07-21 19:16:49

我同意“任何你无法从方法签名中看出的东西”。 它还可能意味着解释方法/函数返回什么。

您可能还想在文档字符串中使用 Sphinx (和 reStructuredText 语法)用于文档目的。 这样您就可以轻松地将其包含在您的文档中。 例如,查看 repoze.bfg ,它广泛使用了这个(示例文件文档示例)。

文档字符串中可以放入的另一件事也是 doctests。 这可能有道理,尤其是。 对于模块或类文档字符串,您还可以以这种方式展示如何使用它并同时使其可测试。

I agree with "Anything that you can't tell from the method's signature". It might also mean to explain what a method/function returns.

You might also want to use Sphinx (and reStructuredText syntax) for documentation purposes inside your docstrings. That way you can include this in your documentation easily. For an example check out e.g. repoze.bfg which uses this extensively (example file, documentation example).

Another thing one can put in docstrings is also doctests. This might make sense esp. for module or class docstrings as you can also show that way how to use it and have this testable at the same time.

萌面超妹 2024-07-21 19:16:49

来自 PEP 8

编写良好文档字符串的约定(又名
“docstrings”)在 PEP 257 中永垂不朽。

  • 为所有公共模块、函数、类和方法编写文档字符串。 文档字符串对于非公共方法不是必需的,但是您
    应该有一个注释来描述该方法的作用。 这
    注释应出现在“def”行之后。
  • PEP 257 描述了良好的文档字符串约定。 请注意,最重要的是,结束多行文档字符串的“””应该位于
    单独一行,并且前面最好有一个空行。
  • 对于单行文档字符串,可以将结尾的“””保留在同一行。

From PEP 8:

Conventions for writing good documentation strings (a.k.a.
"docstrings") are immortalized in PEP 257.

  • Write docstrings for all public modules, functions, classes, and methods. Docstrings are not necessary for non-public methods, but you
    should have a comment that describes what the method does. This
    comment should appear after the "def" line.
  • PEP 257 describes good docstring conventions. Note that most importantly, the """ that ends a multiline docstring should be on a
    line by itself, and preferably preceded by a blank line.
  • For one liner docstrings, it's okay to keep the closing """ on the same line.
我的奇迹 2024-07-21 19:16:49

查看 numpy 的文档字符串以获取很好的示例(例如 http://github.com/numpy/numpy/blob/master/numpy/core/numeric.py" com/numpy/numpy/blob/master/numpy/core/numeric.py)。

文档字符串分为几个部分,如下所示:

Compute the sum of the elements of a list.

Parameters
----------
foo: sequence of ints
   The list of integers to sum up.

Returns
-------
res: int
   sum of elements of foo

See also
--------
cumsum:  compute cumulative sum of elemenents

Check out numpy's docstrings for good examples (e.g. http://github.com/numpy/numpy/blob/master/numpy/core/numeric.py).

The docstrings are split into several sections and look like this:

Compute the sum of the elements of a list.

Parameters
----------
foo: sequence of ints
   The list of integers to sum up.

Returns
-------
res: int
   sum of elements of foo

See also
--------
cumsum:  compute cumulative sum of elemenents
颜漓半夏 2024-07-21 19:16:49

那里应该放什么:

任何你无法从方法签名中看出的东西。 在这种情况下,唯一有用的是:displayName - 如果为空将设置为字段名称。

What should go there:

Anything that you can't tell from the method's signature. In this case the only bit useful is: displayName - if empty will be set to field name.

2024-07-21 19:16:49

我能想到包含在文档字符串中的最引人注目的内容是那些不明显的内容。 通常这包括类型信息或功能要求 - 例如。 “需要一个类似文件的对象”。 在某些情况下,这可以从签名中明显看出,但在其他情况下则不然。

您可以放入文档字符串中的另一个有用的东西是doctest

The most striking things I can think of to include in a docstring are the things that aren't obvious. Usually this includes type information, or capability requirements - eg. "Requires a file-like object". In some cases this will be evident from the signature, not so in other cases.

Another useful thing you can put in to your docstrings is a doctest.

烟沫凡尘 2024-07-21 19:16:49

我喜欢使用文档尽可能详细地描述函数的功能,尤其是极端情况(也称为边缘情况)的行为。 理想情况下,使用该函数的程序员永远不必查看源代码 - 实际上,这意味着每当另一个程序员确实必须查看源代码以弄清楚该函数如何工作的一些细节时,该细节可能应该是文档中提到。 正如 Freddy 所说,任何不向方法签名添加任何细节的内容可能都不应该出现在文档字符串中。

I like to use the documentation to describe in as much detail as possible what the function does, especially the behavior at corner cases (a.k.a. edge cases). Ideally, a programmer using the function should never have to look at the source code - in practice, that means that whenever another programmer does have to look at source code to figure out some detail of how the function works, that detail probably should have been mentioned in the documentation. As Freddy said, anything that doesn't add any detail to the method's signature probably shouldn't be in a documentation string.

过期情话 2024-07-21 19:16:49

在函数开头添加文档字符串的一般目的是描述函数,它做什么,它会返回什么,以及有关参数的描述。 如果需要,您可以添加实施细节。 您甚至可以为未来的开发人员添加有关编写代码的作者的详细信息。

Generally purpose of adding adding doc string in starting of function is to describe function, what it does, what it would return, and description about parameters. You can add implementation details if required. Even you can add details about author who wrote the code for future developer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文