我对python很新。我知道docstrings用于文档,我可以使用help()来调用它们。我的问题是,当我在带有文档的.py文件中编写我自己的函数时,例如:
file - foo.py
def foo():
"""
foo documentation
"""
some code here
如何从终端或交互式会话中打印出“foo文档”?感谢。
答案 0 :(得分:0)
在REPL会话中,可以使用help
而不是直接访问.__doc__
:
>>> def foo():
... """
... foo documentation
... """
>>> help(foo)
Help on function foo:
foo()
foo documentation
>>>
.__doc__
仅将docstring作为原始字符串返回:
>>> foo.__doc__
'\n foo documentation\n '
>>>
答案 1 :(得分:0)
function_name.__doc__
用于获取python中函数的docstring
以下示例是常用的 range
函数的文档字符串
range.__doc__
'range(stop) -> list of integers\nrange(start, stop[, step]) -> list of integers\n\nReturn a list containing an arithmetic progression of integers.\nrange(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\nWhen step is given, it specifies the increment (or decrement).\nFor example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\nThese are exactly the valid indices for a list of 4 elements.'