169

我目前是从 Python 开始的,我有很强的 PHP 背景,在 PHP 中我已经养成了将javadoc其用作文档模板的习惯。

我想知道它是否在 Python 中javadoc作为文档。这里的既定惯例和/或官方准则是什么?docstring

例如,像这样的东西过于复杂,不适合 Python 的思维方式,还是我应该尽量简洁?

"""
replaces template place holder with values

@param string timestamp     formatted date to display
@param string priority      priority number
@param string priority_name priority name
@param string message       message to display

@return string formatted string
"""

如果我有点太详尽了,我应该改用这样的东西吗(大多数文档都没有通过该__doc__方法打印出来)?

# replaces template place holder with values
#    
# @param string timestamp     formatted date to display
# @param string priority      priority number
# @param string priority_name priority name
# @param string message       message to display
#    
# @return string formatted string

def format(self, timestamp = '', priority = '', priority_name = '', message = ''):
    """
    replaces template place holder with values
    """
    values = {'%timestamp%' : timestamp,
              '%priorityName%' : priority_name,
              '%priority%' : priority,
              '%message%' : message}

    return self.__pattern.format(**values)
4

4 回答 4

232

看看reStructuredText(也称为“reST”)格式,它是一种纯文本/文档字符串标记格式,可能是 Python 世界中最流行的格式。你当然应该看看Sphinx,这是一个从 reStructuredText 生成文档的工具(用于例如 Python 文档本身)。Sphinx 包括从代码中的文档字符串中提取文档的可能性(请参阅sphinx.ext.autodoc ),并按照某些约定识别 reST字段列表。这可能已经成为(或正在成为)最流行的方式。

您的示例可能如下所示:

"""Replaces template placeholder with values.

:param timestamp: formatted date to display
:param priority: priority number
:param priority_name: priority name
:param message: message to display
:returns: formatted string
"""

或扩展类型信息:

"""Replaces template placeholder with values.

:param timestamp: formatted date to display
:type timestamp: str or unicode
:param priority: priority number
:type priority: str or unicode
:param priority_name: priority name
:type priority_name: str or unicode
:param message: message to display
:type message: str or unicode
:returns: formatted string
:rtype: str or unicode
"""
于 2011-03-17T13:02:27.763 回答
77

遵循谷歌 Python 风格指南。请注意,Sphinx 还可以使用Napolean扩展来解析这种格式,该扩展将与 Sphinx 1.3 一起打包(这也与PEP257兼容):

def func(arg1, arg2):
    """Summary line.

    Extended description of function.

    Args:
        arg1 (int): Description of arg1
        arg2 (str): Description of arg2

    Returns:
        bool: Description of return value

    """
    return True

示例取自上面链接的拿破仑文档。

这里是所有类型文档字符串的综合示例。

于 2014-12-01T16:10:53.357 回答
25

Python 文档字符串的标准在Python Enhancement Proposal 257中进行了描述。

您的方法的适当评论将类似于

def format(...):
    """Return timestamp string with place holders replaced with values.

    Keyword arguments:
    timestamp     -- the format string (default '')
    priority      -- priority number (default '')
    priority_name -- priority name (default '')
    message       -- message to display (default '')
    """
于 2011-03-17T03:40:14.340 回答
1

查看Documenting Python,这是一个“针对 Python 文档的作者和潜在作者”的页面。

简而言之,reStructuredText用于记录 Python 本身。开发人员指南包含一个 reST 入门、样式指南和编写良好文档的一般建议。

于 2012-07-19T03:31:03.327 回答