在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}
功能。...
您可以在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 docstrings很好地集成在一起,可以为您完成许多手动工作。
是。它们称为文档字符串。参见https://www.python.org/dev/peps/pep-0257/
def foo(bar: int, baz: int) -> int:
"""
Add two numbers
:param foo: explain foo
:param bar: explain bar
:return: int
"""
return bar + baz