简体   繁体   English

如何在 Python 中为返回 None 但在磁盘中写入文件的函数编写文档字符串?

[英]How to write docstring in Python for a function that returns None but write files in disk?

I want to use Python docstring to better document my code.我想使用 Python 文档字符串来更好地记录我的代码。

In the function below, it takes a list as input and write a file into disk.在下面的函数中,它将列表作为输入并将文件写入磁盘。 I want to know how to document the output of function in docstring as the function return type in None.我想知道如何将 docstring 中函数的输出记录为 None 中的函数返回类型。 Does it have a convention?它有约定吗?

def foo(list_of_items: list) -> None :
    """Simple function.

    Parameters
    ----------
        list_of_items : list of str
        list of some texts    

    Returns
    -------
        what is the best way to write this section?
    """

    file = open('/path/output.txt')
    file.write(list_of_items[1])
    file.close()

The only Python convention regarding type hints would be:关于类型提示的唯一 Python 约定是:

Non-goals , PEP 484非目标, PEP 484

It should also be emphasized that Python will remain a dynamically typed language, and the authors have no desire to ever make type hints mandatory, even by convention.还应该强调的是,Python 将仍然是一种动态类型的语言,即使按照惯例,作者也不希望强制类型提示。

Considering this, there are 3 syntax for writing docstrings .考虑到这一点,编写 docstrings3 种语法 Two syntaxes have their own style guide, see numpydoc docstring guide and Google Python Style Guide - 3.8 Comments and Docstrings .两种语法都有自己的风格指南,参见numpydoc docstring guideGoogle Python Style Guide - 3.8 Comments and Docstrings (By comparison PEP 8 and PEP 257 are not as specific). (相比之下, PEP 8PEP 257没有那么具体)。

The case of functions that only return None is not specifically mentioned by numpydoc style guide, carefully notice that it only addressess the case of functions that return values : numpydoc 风格指南没有特别提到只返回None的函数的情况,仔细注意它只解决了返回值函数的情况:

Sections - numpydoc docstring guide 部分 - numpydoc 文档字符串指南

  1. Returns退货

Explanation of the returned values and their types.返回值及其类型的说明。 Similar to the Parameters section, except the name of each return value is optional.类似于参数部分,除了每个返回值的名称是可选的。 The type of each return value is always required每个返回值的类型总是需要的

Thus In formal Python terms, a function that returns None returns no value, because in this situation None signifies the absence of a value , and so the numpydoc guideline does not mention this special case:因此,在正式的 Python 术语中,返回None的函数None返回任何值,因为在这种情况下None表示没有 value ,因此 numpydoc 指南没有提到这种特殊情况:

3.2. 3.2. The standard type hierarchy, Data Model标准类型层次结构,数据模型

None没有任何

  • This type has a single value(...) It is used to signify the absence of a value in many situations, eg, it is returned from functions that don't explicitly return anything.这种类型只有一个 value(...)它用于表示在许多情况下没有值,例如,它是从不显式返回任何内容的函数中返回的。

Since the style guide is omiss, so we can try to search the numpy documentation for a function that returns None to see how it's done.由于缺少样式指南,因此我们可以尝试在 numpy 文档中搜索返回None的函数以查看它是如何完成的。 A good example is numpy.copyto and indeed the docstring section corresponding to Return is absent.一个很好的例子是numpy.copyto并且确实没有对应于Return文档字符串部分

numpy.copyto 文档的图像

The detailed documentation of a method and its docstring should not to be confused with a listing where a brief textual description might be given.方法及其文档字符串的详细文档不应与可能给出简短文本描述的列表混淆。 Compare the detailed documentation of the above numpy.copyto example with the list in Logic functions - comparison which does textually describe the returns but omits all type hints from the signatures.将上述numpy.copyto示例的详细文档与Logic 函数中的列表进行比较 - 比较它确实以文本方式描述了返回但省略了签名中的所有类型提示。

For completeness we can quote the Google style guide on this case:为了完整起见,我们可以在此案例中引用 Google 风格指南:

3.8.3 Functions and Methods, Google Python Style Guide 3.8.3 函数和方法,Google Python 风格指南

Returns: (or Yields: for generators) (...) If the function only returns None, this section is not required. Returns: (or Yields: for generators) (...) 如果函数只返回 None,则不需要此部分。

Finally, the example in the question can be written with NumPy style docstrings as:最后,问题中的示例可以使用 NumPy 样式的文档字符串编写为:

def foo(list_of_items: list, the_path: str) -> None:
    """Simple function writes list_of_items to the_path.

    Parameters
    ----------
        list_of_items : list of str
            Describes list_of_items.
        the_path : str
            Describes the_path.
    """

    file = open(the_path)
    file.write(list_of_items[1])
    file.close()

Or with Google style docstrings as:或者使用 Google 样式的文档字符串作为:

def foo2(list_of_items: list[str], the_path: str) -> None:
    """Simple function writes list_of_items to the_path.

    Params:
        list_of_items (list[str]): Describes list_of_items.
        the_path (str): Describes the_path.
    """

    file = open(the_path)
    file.write(list_of_items[1])
    file.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM