Python编程
Completion requirements
本书对于学习 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 模块的帮助
help(re.sub) # 显示 re 模块中 sub 函数的帮助
help(1) # 显示 int 类型的帮助
help([]) # 显示 list 类型的帮助
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>
。你可以通过输入 modules
、keywords
或 topics
来访问帮助的不同部分。
输入其中一个名称将打印出与该项相关的帮助页面。要获取可用模块、关键字或主题的列表,可以输入 modules
、keywords
或 topics
。每个模块也会附带一个关于它的功能的简短摘要;要列出摘要中包含特定单词(如 "spam")的模块,可以输入 modules spam
。
你可以通过输入 quit
或输入空行来退出帮助系统并返回到解释器。
帮助参数
你可以在不进入交互式帮助的情况下获取特定命令的信息。
例如,你可以通过在引号中添加字符串来获取特定话题的帮助,如 help("object")
。你也可以通过将对象作为参数传递给 help
函数来获取关于给定对象的帮助。
Last modified: Thursday, 30 January 2025, 10:56 PM