自学 Python 不求人,本文介绍 Python 自学时如何获取免费权威的帮助信息。
概述
- 如何使用
Python
自带的help()
方法 - 如何利用
Python
文档 - 获取中文文档
获取帮助 help
每次进入 python 解释器的交互模式时,都会先打印横幅消息,在版本信息下面就是推荐
的命令:
Type "help", "copyright", "credits" or "license" for more information. | |
>>> |
让我们试试:
>>> help | |
Type help() for interactive help, or help(object) for help about object. | |
>>> |
提示使用 help()
进入交互式的 help,或者使用传一个对象给 help(object)
。
交互式 help
不带参数调用 help()
进入到 help
的交互模式:
>>> help() | |
Welcome to Python 3.8's help utility! | |
If this is your first time using Python, you should definitely check out | |
the tutorial on the Internet at https://docs.python.org/3.8/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, symbols, or topics, type | |
"modules", "keywords", "symbols", or "topics". Each module also comes | |
with a one-line summary of what it does; to list the modules whose name | |
or summary contain a given string such as "spam", type "modules spam". | |
help> <--- 开始变成 help 的提示符 | |
这时候可以输入想要查询的任何内容,有几种特定的查询词:
-
modules,显示所有的 模块 列表
-
keywords,显示所有关键字
-
symbols, 显示所有的符号
-
topics,显示一些特定的话题,注意这些话题都是全部大写的,想要继续查询也需要输入全部大写的话题名。
help(thing)
进入交互式模式步骤较多,所以大多少情况下我们直接给 help()
传递一个参数,这个参数就是要查的 东西,比如用来查询 help
自己:
>>> help(help) | |
Help on _Helper in module _sitebuiltins object: | |
class _Helper(builtins.object) | |
| Define the builtin 'help'. | |
| | |
| This is a wrapper around pydoc.help that provides a helpful message | |
| when 'help' is typed at the Python interactive prompt. | |
| | |
| Calling help() at the Python prompt starts an interactive help session. | |
| Calling help(thing) prints help for the python object 'thing'. | |
| | |
| Methods defined here: | |
| | |
| __call__(self, *args, **kwds) | |
| Call self as a function. | |
| | |
| __repr__(self) | |
| Return repr(self). | |
| | |
| ---------------------------------------------------------------------- | |
| Data descriptors defined here: | |
| | |
| __dict__ | |
| dictionary for instance variables (if defined) | |
| | |
| __weakref__ | |
| list of weak references to the object (if defined) |
这个 东西 可以是一个对象,Python 里面一切皆对象,所以它就是任何我们能用的东西(变量、函数、类、模块等),但是这里有个前提是,这个对象必须得在当前的命名空间内能访问,这才能直接用它的名字,比如 sys
模块,要导入后才能查。
会在以后讲解变量的时候详细讨论名字。
如果没有导入就会报错:
>>> help(sys) | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
NameError: name 'sys' is not defined |
所以这种传对象的方式也不能去查关键字和符号,比如:
>>> help(def) | |
File "<stdin>", line 1 | |
help(def) | |
^ | |
SyntaxError: invalid syntax | |
>>> help(+) | |
File "<stdin>", line 1 | |
help(+) | |
^ | |
SyntaxError: invalid syntax |
这时候就需要把查询关键字用引号括起来作为字符串传给 help()
>>> help('sys') | |
>>> help('def') | |
>>> help('+') |
在
help
交互模式下没有此限制
所以,help
的用法很简单,哪里不会查哪里。
help
的帮助信息里面还有关键的一句话:
This is a wrapper around
pydoc.help
that provides a helpful message
when 'help' is typed at the Python interactive prompt. |
我们简单看下 pydoc
pydoc
模块
pydoc
是一个模块,用来从 python 模块自动生成文档(documentation)。
>>> import pydoc | |
>>> pydoc.help.help('sys') # 等价 help('sys') | |
pydoc
提供了命令行操作,不过无法直接使用。用我们学过的 python -m
的启动方式即可:
# 在命令行中执行,不是在 python 交互环境 | |
python -m pydoc | |
pydoc - the Python documentation tool | |
pydoc <name> ... | |
Show text documentation on something. <name> may be the name of a | |
Python keyword, topic, function, module, or package, or a dotted | |
reference to a class or function within a module or module in a | |
package. If <name> contains a '\', it is used as the path to a | |
Python source file to document. If name is 'keywords', 'topics', | |
or 'modules', a listing of these things is displayed. | |
pydoc -k <keyword> | |
Search for a keyword in the synopsis lines of all available modules. | |
pydoc -n <hostname> | |
Start an HTTP server with the given hostname (default: localhost). | |
pydoc -p <port> | |
Start an HTTP server on the given port on the local machine. Port | |
number 0 can be used to get an arbitrary unused port. | |
pydoc -b | |
Start an HTTP server on an arbitrary unused port and open a Web browser | |
to interactively browse documentation. This option can be used in | |
combination with -n and/or -p. | |
pydoc -w <name> ... | |
Write out the HTML documentation for a module to a file in the current | |
directory. If <name> contains a '\', it is treated as a filename; if | |
it names a directory, documentation is written for all the contents. |
这里参数还是比较简单的,普通的使用就和 help
差不多,唯一有点亮点的是可以自动起一个 HTTP 服务器,并自动打开浏览器,显示文档的 html 页面:
python -m pydoc -b |
这里需要说明的是,因为这个帮助系统是通过 module
自动生成的,所以它不仅仅是能显示 Python 标准库的帮助信息。正常安装的第三方库的帮助信息也能一起查询到。
比如说,查一下 requests
:
>>> help('requests') | |
Help on package requests: | |
NAME | |
requests | |
DESCRIPTION | |
Requests HTTP Library | |
~~~~~~~~~~~~~~~~~~~~~ | |
Requests is an HTTP library, written in Python, for human beings. Basic GET | |
usage: | |
>>> import requests | |
>>> r = requests.get('https://www.python.org') | |
>>> r.status_code | |
200 | |
>>> 'Python is a programming language' in r.content | |
True |
Python 文档
打开 安装 Python 时自带的 .chm
帮助文档,可以看到内容非常多。
文档的内容非常详尽,所以也不可能完全看完,我把它们大概分为 4 种:
- 可以通读的:
- 入门教程:初学者首选
- 语言参考:进阶者必看
- 可以选读的:
- 标准库:里面内容是按照话题展开讨论的
- HowTo:类似于 cookbook,里面是特定的任务
- 日常检索(平常最多的时候是通过上面的索引栏查找关键字,有时候不清楚拿什么搜的时候可以看索引表)
- 模块索引:
- 术语表
- FAQ
- 特定目的
- 安装配置
- 安装包:主要就是介绍 pypi 和 venv
- 分发包: 教你如何开发和发布自己的包
- C 语言相关扩展
中文文档
在广大网友的帮助下,Python 文档的中文翻译也进行的不错,虽然还没有到 100% 覆盖,但是大部分还是可以的。
而且其中的内容我挑着看了些,翻译的还是很准确的。
在 Python 在线文档 里可以选择语言为 简体中文,或者直接打开 中文文档链接:
看到左上角还有下载按钮:
下载页面是英文的,别怕,下载下来的文档都是中文的。有 3 种格式:
- pdf 的,细分了两种,我没看出大区别,推荐 A4
pdf 文件是按照大的一级目录划分的,所以可以单个打开,比较适合当书看。
目录书签,还带语法高亮,还要啥自行车。
- html 的,每个文件都是单一的 html,文件数目比较多,不过通过浏览器打开效果和在线没区别
- epub 的,我想转一下到 Kindle 看效果的,结果试了几个工具都报错,有知道的小伙伴可以在评论区留言。
最后
如果对Python感兴趣的话,可以试试我的学习方法以及相关的学习资料
Python所有方向的学习路线
Python所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。
二、Python必备开发工具
三、精品Python学习书籍
当我学到一定基础,有自己的理解能力的时候,会去阅读一些前辈整理的书籍或者手写的笔记资料,这些笔记详细记载了他们对一些技术点的理解,这些理解是比较独到,可以学到不一样的思路。
四、Python视频合集
观看零基础学习视频,看视频学习是最快捷也是最有效果的方式,跟着视频中老师的思路,从基础到深入,还是很容易入门的。
五、实战案例
光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。
六、Python练习题
检查学习结果。
七、面试资料
我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。
大家拿到脑图后,根据脑图对应的学习路线,做好学习计划制定。根据学习计划的路线来逐步学习,正常情况下2个月以内,再结合文章中资料,就能够很好地掌握Python并实现一些实践功能。