这本书对于学习 Python 很有用,但书中可能没有涵盖某个主题。您可能想在标准库中搜索模块,或检查未知对象的函数,或者您可能知道对象内部有一个必须调用的函数,但不知道其名称。这就是交互式帮助发挥作用的地方。

内建帮助概览

编辑
help() # 启动交互式帮助
help("topics") # 输出帮助主题列表
help("OPERATORS") # 显示运算符主题的帮助
help("len") # 显示 len 函数的帮助
help("re") # 显示 re 模块的帮助
help("re.sub") # 显示 re 模块的 sub 函数的帮助
help(len) # 显示传递的对象(len 函数)的帮助
help([].pop) # 显示列表的 pop 函数的帮助
dir([]) # 输出列表的属性列表,其中包括函数
import re
help(re) # 显示帮助模块的帮助
help(re.sub) # 显示 re 模块的 sub 函数的帮助
help(1) # 显示 int 类型的帮助
help([]) # 显示列表类型的帮助
help(def) # 失败:def 是一个不引用对象的关键字
help("def") # 显示函数定义的帮助

浏览帮助

编辑

要启动 Python 的交互式帮助,请在提示符下键入“help()”。

>>>help()

您将看到问候语和帮助系统的简要介绍。对于 Python 2.6,提示符将如下所示:

Welcome to Python 2.6! This is the online help utility.

If this is your first time using Python, you should definitely check out
the tutorial on the Internet at http://docs.python.org/tutorial/.

Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules. To quit this help utility and
return to the interpreter, just type "quit".

To get a list of available modules, keywords, or topics, type "modules",
"keywords", or "topics". Each module also comes with a one-line summary
of what it does; to list the modules whose summaries contain a given word
such as "spam", type "modules spam".

还要注意,提示符将从“>>>”(三个右尖括号)更改为“help>”

您只需键入 moduleskeywordstopics 即可访问帮助的不同部分。

键入其中一个名称将打印与相关项目相关的帮助页面。要获取可用模块、关键字或主题的列表,请键入“modules”、“keywords”或“topics”。每个模块还附带一行摘要,说明其功能;要列出摘要中包含给定单词(例如“垃圾邮件”)的模块,请键入“modules spam”。

您可以通过键入“quit”或输入空白行返回解释器来退出帮助系统。

Help参数

编辑

您无需进入交互式帮助即可获取有关特定命令的信息。

例如,只需在引号中添加字符串即可获取有关给定主题的帮助,例如 help("object")。您还可以通过将给定对象作为参数传递给帮助函数来获取有关给定对象的帮助。

外部链接

编辑
  • help in 2. Built-in Functions, docs.python.org