Search code examples
javascriptpythonnode.jspython-3.xdocstring

does python have an equivalent function comment to javascript?


In Javascript the coder can comment functions as follows using the @param and {string} options.

Python has a docstring, but reading the https://www.python.org/dev/peps/pep-0257/ Docstring Conventions i cannot see the equivalent to js.

Here is an example of a commented JS function:

/**
 * 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
}

What would be the python equivalent of the above comment (if such exists) ? In particular the @param and {number} features....


Solution

  • In python you will comment like this in the docstring.

    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
    

    There is several guideline have a look to this anwser.