return 之前的冒号 : 的作用是什么?这是 Python 中的风格吗?
Run Code Online (Sandbox Code Playgroud)def close(self): """ Close this connection. :returns: None """ self.conn.close() return
因为它位于文档字符串中(用三引号括起来),所以它就像注释一样,除了记录代码之外不执行任何操作。
编写文档字符串有多种约定 - 这是其中之一(Sphinx使用的 reST 格式)。
冒号通常用于描述函数需要什么参数以及函数返回什么,如下所示:
"""
This is a reST style.
:param param1: this is a first param
:param param2: this is a second param
:returns: this is a description of what is returned
:raises keyError: raises an exception
"""
Run Code Online (Sandbox Code Playgroud)
在这种情况下,它表示该函数预计返回 None。
有关各种约定的更多详细信息,请参阅这篇文章。