写Python程序肯定避免不了大量的查询资料和帮助文档。帮助文档中毫无疑问Python官方的文档是最权威最准确的。
Python在帮助文档方面做的非常细致和贴心。
首先,可以访问https://docs.python.org/3/查看最新Python3版本的说明文档,在线浏览、搜索和查询关键字,获取你想要的信息。
其次,你也可以从官网下载文档到本地,实现离线浏览,随身携带。
最后,在你安装Python的时候,本地就已经给你提供了一份文档,windows中你可以查看一下程序文件夹,其中的Manuals和Module Docs就是相关文档。
除了单独的帮助文件,Python还很贴心的在解释器内部提供了很多有用的方法,可以直接查看帮助信息,这免去了我们频繁打开、切换帮助文档的过程。
比如内置的help()
帮助函数和dir()
显示成员函数。help()
能够打印出指定方法、函数、变量、类的说明信息。dir()
能够列出对象的所有成员,帮助你快速查找方法和变量。
举例说明,在IDLE中,首先导入你想要查看的对象,比如这里的“timeit”模块,import timeit
,然后调用help(timeit)
和dir(timeit)
。
>>> import timeit >>> dir(timeit) ['Timer', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_globals', 'default_number', 'default_repeat', 'default_timer', 'dummy_src_name', 'gc', 'itertools', 'main', 'reindent', 'repeat', 'sys', 'template', 'time', 'timeit'] >>> help(timeit) Help on module timeit: NAME timeit - Tool for measuring execution time of small code snippets. DESCRIPTION This module avoids a number of common traps for measuring execution times. See also Tim Peters' introduction to the Algorithms chapter in the Python Cookbook, published by O'Reilly.
还可以通过__doc__
成员查看对象的帮助文档,例如:
>>> timeit.__doc__ # 查看模块的说明 "Tool for measuring execution time of small code snippets.\n\nThis module avoids a number of common traps for measuring execution\ntimes. See also Tim Peters' introduction to the Algorithms chapter in\nthe Python Cookbook, published by O'Reilly…….后面省略 >>> timeit.timeit.__doc__ # 查看方法的说明 'Convenience function to create Timer object and call timeit method.‘ >>> print(timeit.__doc__) # 美观打印 Tool for measuring execution time of small code snippets. This module avoids a number of common traps for measuring execution times. See also Tim Peters' introduction to the Algorithms chapter in the Python Cookbook, published by O'Reilly. Library usage: see the Timer class. Command line usage: python timeit.py [-n N] [-r N] [-s S] [-t] [-c] [-p] [-h] [--] [statement] …….后面省略
养成经常查看说明文档的习惯是个好的开始,但是对你的英语能力有一定的要求。