没有任何回复时的文档字符串

Ric*_*son 22 python return docstring function

当函数没有返回任何内容时,docstring约定是什么?

例如:

def f(x):
    """Prints the element given as input

    Args:
        x: any element
    Returns:
    """
    print "your input is %s" % x
    return
Run Code Online (Sandbox Code Playgroud)

Returns:在docstring中我应该添加什么?没有现在的样子?

iCo*_*dez 28

您应该使用None,因为这是您的函数实际返回的内容:

"""Prints the element given as input

Args:
    x: any element
Returns:
    None
"""
Run Code Online (Sandbox Code Playgroud)

所有的Python函数返回的东西.如果您没有显式返回值,那么它们将None默认返回:

>>> def func():
...     return
...
>>> print func()
None
>>>
Run Code Online (Sandbox Code Playgroud)

  • 如果你的意思是"如果你没有任何明确的返回语句,那么完全从你的文档字符串中省略'返回:'是错误的吗?",我暂时说"不".[PEP 257](https://www.python.org/dev/peps/pep-0257/)说要描述返回值"如果适用",所以你可以说你不必描述它,如果它无所谓. (3认同)