小编给大家分享一下查看python模块的方法,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

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

通用帮助函数help()

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

>>>help()WelcometoPython3.5'shelputility!IfthisisyourfirsttimeusingPython,youshoulddefinitelycheckoutthetutorialontheInternetathttp://docs.python.org/3.5/tutorial/.Enterthenameofanymodule,keyword,ortopictogethelponwritingPythonprogramsandusingPythonmodules.Toquitthishelputilityandreturntotheinterpreter,justtype"quit".Togetalistofavailablemodules,keywords,symbols,ortopics,type"modules","keywords","symbols",or"topics".Eachmodulealsocomeswithaone-linesummaryofwhatitdoes;tolistthemoduleswhosenameorsummarycontainagivenstringsuchas"spam",type"modulesspam".help>

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

help>modulesPleasewaitamomentwhileIgatheralistofallavailablemodules...AutoComplete_pyiofilecmppyscreezeAutoCompleteWindow_randomfileinputpytweening......Enteranymodulenametogetmorehelp.Or,type"modulesspam"tosearchformoduleswhosenameorsummarycontainthestring"spam".

模块帮助查询

查看.py结尾的普通模块help(module_name)

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

>>>importmath>>>help(math)Helponbuilt-inmodulemath:NAMEmathDESCRIPTIONThismoduleisalwaysavailable.ItprovidesaccesstothemathematicalfunctionsdefinedbytheCstandard.FUNCTIONSacos(...)acos(x)Returnthearccosine(measuredinradians)ofx....>>>

查看内建模块sys.bultin_modulenames

>>>importsys>>>sys.builtin_module_names('_ast','_bisect','_codecs','_codecs_cn','_codecs_hk',...'zlib')>>>

查询函数信息

查看模块下所有函数dir(module_name)

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

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

以上是查看python模块的方法的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!