pycharm“”“:返回:”“”在Python文档字符串中

问题描述 投票:-1回答:2

当您使用Pycharm编辑py文件时,它会插入这样的模板。

def check_caller_log():
    """
    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    :return: 
    """

:return:的目的是什么?

python return pycharm
2个回答
3
投票

它描述了函数返回的类型(例如,如果它返回一个字符串,则编写return: str)。您还可以添加有关此返回值包含的内容或其用途的注释。

def my_function():
    """
    Some good explanation of what this function does

    :return: str
    """

注意:如果你的函数没有返回任何东西,它实际上返回None,你可以把它写下来:return: None

def my_function():
    """
    Some good explanation of what this function does

    :return: None
    """

0
投票

正如stefan.stt的回答所说,:return:字段允许您提供函数返回值的描述。但是,stefan.stt的示例显示:return:用于记录返回类型而不是描述值。要记录类型,使用:rtype:字段更常见,因此:

def user_name():
    """
    Description of the function goes here.

    :return: the name of the currently active user
    :rtype: str
    """

字段名称没有完全标准化,:returns:也被广泛用于记录返回值;我的印象是:returns:是两个同义词中比较常用的,但是PyCharm的“Python集成工具”已经在:result:上进行了标准化。广泛使用的Python文档生成器Sphinx supports both variants,但其他文档工具可能不会。

值得注意的是,有几种不同的语法用于在Python文档字符串中记录参数,返回值等。最常见的记录在this answer中。

© www.soinside.com 2019 - 2024. All rights reserved.