python - 将 javadoc 用于 Python 文档

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

我想知道 javadoc 是否在 Python 中作为 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)

最佳答案

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

您的示例可能如下所示:

"""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
"""

https://stackoverflow.com/questions/5334531/

相关文章:

linux - 如何在命令行中从脚本运行函数?

python - 获取 MAC 地址

linux - 在 Bash 中,如何在文件的每一行之后添加一个字符串?

python - 从 Pandas 日期时间列中分别提取月份和年份

linux - 附加到进程输出以供查看

linux - grep 的基本表达式不支持\d 吗?

python - 从函数中获取文档字符串

python - 尽管我调用了 pyplot.show(),但 matplotlib 没有显示我的情

linux - Linux bash 中波浪号的含义(不是主目录)

linux - dd:如何计算最佳 block 大小?