使用javadoc for Python文档

JF *_*ion 158 python documentation javadoc docstring

我目前从Python开始,我有一个强大的PHP背景,在PHP中我习惯使用javadoc作为文档模板.

我想知道它是否javadoc有它作为docstringPython文档的位置.这里有既定的惯例和/或官方的guildelines?

例如,这样的东西太精巧,不适合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
"""
Run Code Online (Sandbox Code Playgroud)

如果我有点过于详尽,我应该选择这样的东西(大多数文档不通过该__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)
Run Code Online (Sandbox Code Playgroud)

Ste*_*ven 224

看一下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
"""
Run Code Online (Sandbox Code Playgroud)

或扩展类型信息:

"""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
"""
Run Code Online (Sandbox Code Playgroud)

  • 如果你需要打破一条长长的描述,你会怎么做?那怎么样? (7认同)
  • 请参阅reStructuredText参考,特别是字段列表:http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html#field-lists (6认同)
  • 请注意,此处的短语不符合[PEP 257](https://www.python.org/dev/peps/pep-0257/).它应该是`用模板替换模板占位符.而不是`用值替换模板占位符' - 注意句子,开头的大写字母和结尾的句号(.). (6认同)
  • 有人可以请指出最好的文档,指定这些特殊的文档字符串,如":param ____:"和":returns:"?这样的文件目前似乎很难找到. (2认同)

con*_*d00 74

关注Google 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
Run Code Online (Sandbox Code Playgroud)

从上面链接的Napolean文档中获取的示例.

这里有关于所有类型文档字符串的综合示例.

  • 对于那些读过docstrings的人来说 (20认同)

srg*_*erg 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 '')
    """
Run Code Online (Sandbox Code Playgroud)

  • PEP257没有告诉参数部分的实际格式.它只是说它应该写,并给出一个例子.但这只是一个例子.因此,我明确建议使用Sphinx约定,因为您不会破坏PEP257并使用可由sphinx解析的格式. (17认同)
  • 除了上面提到的第一个文档是丑陋的,并为人类提供了大量冗余信息.我宁愿使用一种惯例,使我的**源代码**阅读愉快而不首先解析 (7认同)