參考鏈接:https://blog.csdn.net/u013810296/article/details/55509284
這里介紹下python自帶的查看幫助功能,可以在編程時不中斷地迅速找到所需模塊和函數的使用方法
查看方法
通用幫助函數help()
python中的
help()類似unix中的man指令,熟悉后會對我們的編程帶來很大幫助
進入help幫助文檔界面,根據屏幕提示可以繼續鍵入相應關鍵詞進行查詢,繼續鍵入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".
可以繼續鍵入相應的模塊名稱得到該模塊的幫助信息。
這是python的通用的查詢幫助,可以查到幾乎所有的幫助文檔,但我們很多時候不需要這樣層級式地向下查詢,接下來會介紹如何直接查詢特定的模塊和函數幫助信息。
例如要查詢math模塊的使用方法,可以如下操作:(輸出的多行信息可通過q鍵退出)
>>> help(math)
使用
help(module_name)時首先需要import該模塊,有些教程中不進行導入而在模塊名中加入引號help('module_name'),這種方法可能會帶來問題,大家可以用math模塊測試,建議使用先導入再使用help()函數查詢
查看內建模塊sys.bultin_modulenames
>>> import sys
>>> sys.builtin_module_names
('_ast', '_bisect', '_codecs', '_codecs_cn', '_codecs_hk', ... 'zlib')
>>>
查詢函數信息
查看模塊下所有函數dir(module_name)
如我們需要列舉出math模塊下所有的函數名稱,同樣需要首先導入該模塊
>>> dir(math) ['__doc__', '__loader__', '__name__',...] >>>
查看模塊下特定函數信息help(module_name.func_name)
注意
func_name后面不要加(),因為python的語法默認加了括號后就運行函數
相應的模塊要導入
>>> help(math.sin)
Help on built-in function sin in module math:
sin(...)
sin(x)
Return the sine of x (measured in radians).
>>>
#例2
>>> help(random.randint())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: randint() missing 2 required positional arguments: 'a' and 'b'
>>> help(random.randint)
Help on method randint in module random:
randint(a, b) method of random.Random instance
Return random integer in range [a, b], including both end points.
Python導入的包可以通過bagname.__all__查看所有方法但是這個有時不太好用,通過help(bagname.funcname)查看方法介紹
>>> help(random.seed)
Help on method seed in module random:
seed(a=None, version=2) method of random.Random instance
Initialize internal state from hashable object.
None or no argument seeds from current time or from an operating
system specific randomness source if available.
If *a* is an int, all bits are used.
For version 2 (the default), all of the bits are used if *a* is a str,
bytes, or bytearray. For version 1 (provided for reproducing random
sequences from older versions of Python), the algorithm for str and
bytes generates a narrower range of seeds.
