def sq(n):"""Return the square of n."""return n * n
结束:
def sq(n):"""Returns the square of n."""return n * n
并且倾向于在较长的文档字符串中不评论第一行:
def sq(n):"""Return the square of n, accepting all numeric types:
>>> sq(10)100
>>> sq(10.434)108.86835599999999
Raises a TypeError when input is invalid:
>>> sq(4*'435')Traceback (most recent call last):...TypeError: can't multiply sequence by non-int of type 'str'
"""return n*n
def square_root(n):"""Calculate the square root of a number.
Args:n: the number to get the square root of.Returns:the square root of n.Raises:TypeError: if n is not a number.ValueError: if n is negative.
"""pass
def func(arg1, arg2):"""Summary line.
Extended description of function.
Parameters----------arg1 : intDescription of arg1arg2 : strDescription of arg2
Returns-------boolDescription of return value
See Also--------otherfunc : some related other function
Examples--------These are written in doctest format, and should illustrate how touse the function.
>>> a=[1,2,3]>>> print [x + 3 for x in a][4, 5, 6]"""return True
"""This is a javadoc style.
@param param1: this is a first param@param param2: this is a second param@return: this is a description of what is returned@raise keyError: raises an exception"""
"""This is a reST style.
:param param1: this is a first param:param param2: this is a second param:returns: this is a description of what is returned:raises keyError: raises an exception"""
"""This is an example of Google style.
Args:param1: This is the first param.param2: This is a second param.
Returns:This is a description of what is returned.
Raises:KeyError: Raises an exception."""
"""My numpydoc description of a kindof very exhautive numpydoc format docstring.
Parameters----------first : array_likethe 1st param name `first`second :the 2nd paramthird : {'value', 'other'}, optionalthe 3rd param, by default 'value'
Returns-------stringa value in a string
Raises------KeyErrorwhen a key errorOtherErrorwhen an other error"""