龙空技术网

Python中的帮助函数

少儿编程Prog61 87

前言:

现时姐妹们对“python中帮助用法”大概比较看重,小伙伴们都想要知道一些“python中帮助用法”的相关内容。那么小编同时在网上汇集了一些有关“python中帮助用法””的相关知识,希望你们能喜欢,看官们一起来了解一下吧!

python help函数用于显示模块,函数,类,关键字等的文档。

帮助函数具有以下语法:

help([object])

如果传递帮助函数时没有参数,则交互式帮助实用程序将在控制台上启动。

让我们在python控制台中检查print函数的文档。

help(print) 

它提供以下输出:

Help on built-in function print in module builtins:print(...)    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)    Prints the values to a stream, or to sys.stdout by default.    Optional keyword arguments:    file:  a file-like object (stream); defaults to the current sys.stdout.    sep:   string inserted between values, default a space.    end:   string appended after the last value, default a newline.    flush: whether to forcibly flush the stream.

也可以为用户定义的函数和类定义帮助函数输出。docstring(文档字符串)用于文档。它嵌套在三个引号中,是类、函数或模块中的第一个语句。

让我们定义一个带有函数的类:

class Helper:     def __init__(self):         '''The helper class is initialized'''      def print_help(self):         '''Returns the help description'''        print('helper description')   help(Helper) help(Helper.print_help) 

在运行上述程序时,我们得到第一个帮助函数的输出,如下所示:

Help on class Helper in module __main__:class Helper(builtins.object) |  Methods defined here: |   |  __init__(self) |      The helper class is initialized |   |  print_help(self) |      Returns the help description |   |  ---------------------------------------------------------------------- |  Data descriptors defined here: |   |  __dict__ |      dictionary for instance variables (if defined) |   |  __weakref__ |      list of weak references to the object (if defined)Help on function print_help in module __main__:print_help(self)    Returns the help description

标签: #python中帮助用法 #python 帮助命令