经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Python » 查看文章
python模块和函数帮助文档快速查看方法示例
来源:jb51  时间:2022/6/27 16:57:50  对本文有异议

引言

python的一个优势是有着大量自带和在线的模块(module)资源,可以提供丰富的功能,在使用这些模块的时候,如果每次都去网站找在线文档会过于耗费时间,结果也不一定准确。因此这里介绍下python自带的查看帮助功能,可以在编程时不中断地迅速找到所需模块和函数的使用方法

通用帮助函数help()

在python命令行中键入help(),可以看到:

  1. >>> help()
  2. Welcome to Python 3.5's help utility!
  3. If this is your first time using Python, you should definitely check out
  4. the tutorial on the Internet at http://docs.python.org/3.5/tutorial/.
  5. Enter the name of any module, keyword, or topic to get help on writing
  6. Python programs and using Python modules. To quit this help utility and
  7. return to the interpreter, just type "quit".
  8. To get a list of available modules, keywords, symbols, or topics, type
  9. "modules", "keywords", "symbols", or "topics". Each module also comes
  10. with a one-line summary of what it does; to list the modules whose name
  11. or summary contain a given string such as "spam", type "modules spam".
  12. help>

进入help帮助文档界面,根据屏幕提示可以继续键入相应关键词进行查询,继续键入modules可以列出当前所有安装的模块:

  1. help> modules
  2. Please wait a moment while I gather a list of all available modules...
  3. AutoComplete _pyio filecmp pyscreeze
  4. AutoCompleteWindow _random fileinput pytweening
  5. ......
  6. Enter any module name to get more help. Or, type "modules spam" to search
  7. for modules whose name or summary contain the string "spam".

可以继续键入相应的模块名称得到该模块的帮助信息。

这是python的通用的查询帮助,可以查到几乎所有的帮助文档,但我们很多时候不需要这样层级式地向下查询,接下来会介绍如何直接查询特定的模块和函数帮助信息。

模块帮助查询

查看.py结尾的普通模块

help(module_name)

例如要查询math模块的使用方法,可以如下操作:

  1. >>> import math
  2. >>> help(math)
  3. Help on built-in module math:
  4. NAME
  5. math
  6. DESCRIPTION
  7. This module is always available. It provides access to the
  8. mathematical functions defined by the C standard.
  9. FUNCTIONS
  10. acos(...)
  11. acos(x)
  12. Return the arc cosine (measured in radians) of x.
  13. ...
  14. >>>

使用help(module_name)时首先需要import该模块,有些教程中不进行导入而在模块名中加入引号help('module_name'),这种方法可能会带来问题,大家可以用math模块测试,建议使用先导入再使用help()函数查询

查看内建模块

sys.bultin_modulenames

  1. >>> import sys
  2. >>> sys.builtin_module_names
  3. ('_ast', '_bisect', '_codecs', '_codecs_cn', '_codecs_hk', ... 'zlib')
  4. >>>

需要导入sys模块。这里列举的一般是自带的使用C/C++编译链接的模块

查询函数信息

查看模块下所有函数

dir(module_name)

如我们需要列举出math模块下所有的函数名称

  1. >>> dir(math)
  2. ['__doc__', '__loader__', '__name__',...]
  3. >>>

同样需要首先导入该模块

查看模块下特定函数信息

help(module_name.func_name)

如查看math下的sin()函数

  1. >>> help(math.sin)
  2. Help on built-in function sin in module math:
  3. sin(...)
  4. sin(x)
  5. Return the sine of x (measured in radians).
  6. >>>

查看函数信息的另一种方法

print(func_name.__doc__)

如查看内建函数print用法

  1. >>> print(print.__doc__)
  2. print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
  3. Prints the values to a stream, or to sys.stdout by default.
  4. ...
  5. >>>

__doc__前后是两个短下划线,在python中会合并为长下划线

python中的help()类似unix中的man指令,熟悉后会对我们的编程带来很大帮助

以上就是python模块和函数帮助文档快速查看方法示例的详细内容,更多关于查看python模块函数帮助文档的资料请关注w3xue其它相关文章!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号