【问题标题】:does python have an equivalent function comment to javascript?python 有与 javascript 等效的函数注释吗?
【发布时间】:2019-11-05 11:47:40
【问题描述】:

在 Javascript 中,编码人员可以使用 @param{string} 选项对函数进行如下注释。

Python 有一个文档字符串,但阅读 https://www.python.org/dev/peps/pep-0257/ 文档字符串约定我看不到与 js 等效的内容。

下面是一个带注释的 JS 函数的例子:

/**
 * generate a random matrix
 * @param {number} n the height of the matrix
 * @param {number} m the width of the matrix
 */
function generateRandomMatrix(n, m) {
    mtrx = []
    for (let i = 0; i < n; i++) {
        mtrx.push([])
        for (let j = 0; j < m; j++) {
            mtrx[i].push(Math.round(Math.random()*10))
        }
    }
    return mtrx
}

上述 comment 的 python 等价物是什么(如果存在的话)? 特别是@param{number} 功能......

【问题讨论】:

    标签: javascript python node.js python-3.x docstring


    【解决方案1】:

    在python中,你会在文档字符串中这样评论。

    def generate_random_matrix(n, m):
        """generate a random matrix
    
         Parameters
         -----------------
         n : int
             the height of the matrix
         m : int 
             the width of the matrix
    
         Returns
         ----------
         An array with shape (n, m)
        """
        pass
    

    有几个指南看看这个anwser

    【讨论】:

    • 这似乎是最接近“正确”的。
    • 什么意思?
    • 发布了 3 个答案,似乎是其中最合适的。
    • @D.L 我已经更新了我的 anwser,其中包含指向 python 中文档格式概述的链接。随意点击投票按钮下方的复选标记来验证我的遮阳篷。
    • 谢谢提醒。我现在正在使用这种格式进行编码。完成。
    【解决方案2】:

    关于 Python 中函数参数的注释也应该包含在文档字符串中,然后您可以使用 Sphinx 自动生成文档。 Sphinx 最初是为 Python 文档本身创建的。

    默认情况下,Sphinx 采用以下格式的文档字符串(参见here):

    :param [ParamName]: [ParamDescription], defaults to [DefaultParamVal]
    :type [ParamName]: [ParamType](, optional)
    ...
    :raises [ErrorType]: [ErrorDescription]
    ...
    :return: [ReturnDescription]
    :rtype: [ReturnType]
    

    但是您可以使用Napoleon extension for Sphinx 来阅读更具可读性(因此也是Pythonic)Google Style Docstrings

    def fetch_bigtable_rows(big_table, keys, other_silly_variable=None):
        """Fetches rows from a Bigtable.
    
        Retrieves rows pertaining to the given keys from the Table instance
        represented by big_table.  Silly things may happen if
        other_silly_variable is not None.
    
        Args:
            big_table: An open Bigtable Table instance.
            keys: A sequence of strings representing the key of each table row
                to fetch.
            other_silly_variable: Another optional variable, that has a much
                longer name than the other args, and which does nothing.
        """
    

    【讨论】:

    • 这非常好(实际上是最接近的),但需要扩展 [Napoleon extension for Sphinx] 所以现在选择不同的答案作为首选。
    • 不需要扩展来编写这样的文档字符串,只是为了让 Sphinx 的自动文档能够识别您正在编写的内容的含义。 @florian-bernard 为您提供了一个使用 NumPy style 的示例,如果您希望 Sphinx 编写文档,您还需要 Napoleon 扩展。
    • 当然,如果您不希望 Sphinx 编写您的文档,那么还没有一个公认的 Python 标准。但是自从"There should be one-- and preferably only one --obvious way to do it"之后,我怀疑在某个时候会有一个标准......
    • 我勾选的方法在语法上是最接近的。这不需要任何扩展。已经在 VS 代码(流行的编辑器)中进行了测试,并且在调用函数帮助时显示良好(将鼠标悬停在函数名称上)。
    【解决方案3】:

    是的。它们被称为文档字符串。见https://www.python.org/dev/peps/pep-0257/

    def foo(bar: int, baz: int) -> int:
        """
        Add two numbers
    
        :param bar: explain bar
        :param baz: explain baz
        :return: int
        """
        return bar + baz
    

    【讨论】:

      【解决方案4】:

      Python 有两种类型的 cmets。一个用于 sn-ps,或单行 cmets (#),另一个用于多个。

      你需要的是第二个:

      def pyhton_function(parameter1):
      """
      This type of comment is preferable for longer text and function description.
      
      Function returns double the parameter 1 received
      
      """
      return parameter1
      

      可能值得注意的是,“”中的任何内容都不能是评论。 即:

      print("#this is not a comment")
      

      【讨论】:

      • 这是正确的。但是,并没有解决复制 JS 注释格式的问题。
      【解决方案5】:

      你可以用 Python 写这样的东西:

      def get_full_name(first_name, last_name):
          """
          Construct full name from last name and first name
      
          :param first_name: first name of Person 
          :param last_name: last name of Person
          :return: concatenation of first and last name of Person
          """
          return first_name + last_name
      

      Pycharm 与 python 文档字符串有很好的集成,可以为你做很多手动工作。有很多可能的格式。上面的示例显示了 Sphinx 用于生成文档的可能更普遍的格式。

      看看这个好描述:What is the standard Python docstring format?

      【讨论】:

      • docstring 规定顶行和其他行之间需要空格。重点是复制(或改进)js 中存在的内容。
      猜你喜欢
      • 1970-01-01
      • 2010-12-14
      • 2011-06-18
      • 2011-06-25
      • 2017-09-11
      • 2015-12-26
      • 2017-02-14
      相关资源
      最近更新 更多