在python中查看模塊功能的方法

這篇文章將為大家詳細講解有關在python中查看模塊功能的方法,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

創(chuàng)新互聯(lián)主要從事成都做網(wǎng)站、成都網(wǎng)站設計、網(wǎng)頁設計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務。立足成都服務岫巖,10年網(wǎng)站建設經(jīng)驗,價格優(yōu)惠、服務專業(yè),歡迎來電咨詢建站服務:18982081108

在python中查看模塊功能的方法:1、在python命令行輸入help()函數(shù)進入help幫助文檔界面;2、鍵入【modules】列出當前所有安裝的模塊;3、鍵入相應的模塊名稱即可得到該模塊的功能信息。

在python中查看模塊功能的方法

python的一個優(yōu)勢是有著大量自帶和在線的模塊(module)資源,可以提供豐富的功能,在使用這些模塊的時候,如果每次都去網(wǎng)站找在線文檔會過于耗費時間,結(jié)果也不一定準確。因此這里介紹下python自帶的查看幫助功能,可以在編程時不中斷地迅速找到所需模塊和函數(shù)的使用方法

通用幫助函數(shù)help()

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

>>> help()
Welcome to Python 3.5's 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/3.5/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幫助文檔界面,根據(jù)屏幕提示可以繼續(xù)鍵入相應關鍵詞進行查詢,繼續(xù)鍵入modules可以列出當前所有安裝的模塊:

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

可以繼續(xù)鍵入相應的模塊名稱得到該模塊的幫助信息。

這是python的通用的查詢幫助,可以查到幾乎所有的幫助文檔,但我們很多時候不需要這樣層級式地向下查詢,接下來會介紹如何直接查詢特定的模塊和函數(shù)幫助信息。

模塊幫助查詢

查看.py結(jié)尾的普通模塊help(module_name)

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

>>> import math
>>> help(math)
Help on built-in module math:
NAME
    math
DESCRIPTION
    This module is always available.  It provides access to the
    mathematical functions defined by the C standard.
FUNCTIONS
    acos(...)
        acos(x)
        Return the arc cosine (measured in radians) of x.
...
>>>

使用help(module_name)時首先需要import該模塊,有些教程中不進行導入而在模塊名中加入引號help('module_name'),這種方法可能會帶來問題,大家可以用math模塊測試,建議使用先導入再使用help()函數(shù)查詢

查看內(nèi)建模塊sys.bultin_modulenames

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

需要導入sys模塊。這里列舉的一般是自帶的使用C/C++編譯鏈接的模塊

查詢函數(shù)信息

查看模塊下所有函數(shù)dir(module_name)

如我們需要列舉出math模塊下所有的函數(shù)名稱

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

同樣需要首先導入該模塊

查看模塊下特定函數(shù)信息help(module_name.func_name)

如查看math下的sin()函數(shù)

>>> help(math.sin)
Help on built-in function sin in module math:
sin(...)
    sin(x)
    Return the sine of x (measured in radians).
>>>

查看函數(shù)信息的另一種方法print(func_name.__doc__)

如查看內(nèi)建函數(shù)print用法

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

__doc__前后是兩個短下劃線,在python中會合并為長下劃線

python中的help()類似unix中的man指令,熟悉后會對我們的編程帶來很大幫助

關于在python中查看模塊功能的方法就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

本文題目:在python中查看模塊功能的方法
網(wǎng)址分享:http://muchs.cn/article12/jpggdc.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供面包屑導航、外貿(mào)建站、Google、網(wǎng)站制作云服務器、靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名