網(wǎng)站首頁 編程語言 正文
引言
python的一個(gè)優(yōu)勢(shì)是有著大量自帶和在線的模塊(module)資源,可以提供豐富的功能,在使用這些模塊的時(shí)候,如果每次都去網(wǎng)站找在線文檔會(huì)過于耗費(fèi)時(shí)間,結(jié)果也不一定準(zhǔn)確。因此這里介紹下python自帶的查看幫助功能,可以在編程時(shí)不中斷地迅速找到所需模塊和函數(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>
進(jìn)入help幫助文檔界面,根據(jù)屏幕提示可以繼續(xù)鍵入相應(yīng)關(guān)鍵詞進(jìn)行查詢,繼續(xù)鍵入modules可以列出當(dāng)前所有安裝的模塊:
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ù)鍵入相應(yīng)的模塊名稱得到該模塊的幫助信息。
這是python的通用的查詢幫助,可以查到幾乎所有的幫助文檔,但我們很多時(shí)候不需要這樣層級(jí)式地向下查詢,接下來會(huì)介紹如何直接查詢特定的模塊和函數(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)時(shí)首先需要import該模塊,有些教程中不進(jìn)行導(dǎo)入而在模塊名中加入引號(hào)help('module_name'),這種方法可能會(huì)帶來問題,大家可以用math模塊測(cè)試,建議使用先導(dǎo)入再使用help()函數(shù)查詢
查看內(nèi)建模塊
sys.bultin_modulenames
>>> import sys
>>> sys.builtin_module_names
('_ast', '_bisect', '_codecs', '_codecs_cn', '_codecs_hk', ... 'zlib')
>>>
需要導(dǎo)入sys模塊。這里列舉的一般是自帶的使用C/C++編譯鏈接的模塊
查詢函數(shù)信息
查看模塊下所有函數(shù)
dir(module_name)
如我們需要列舉出math模塊下所有的函數(shù)名稱
>>> dir(math)
['__doc__', '__loader__', '__name__',...]
>>>
同樣需要首先導(dǎo)入該模塊
查看模塊下特定函數(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__前后是兩個(gè)短下劃線,在python中會(huì)合并為長下劃線
python中的help()類似unix中的man指令,熟悉后會(huì)對(duì)我們的編程帶來很大幫助
原文鏈接:https://www.jianshu.com/p/0ca80c5377c8
相關(guān)推薦
- 2022-06-15 Go?interface?接口的最佳實(shí)踐經(jīng)驗(yàn)分享_Golang
- 2022-03-16 Quartz.Net使用方法詳解_C#教程
- 2022-04-11 css實(shí)現(xiàn)左邊div固定寬度,右邊div自適應(yīng)撐滿剩下的寬度
- 2023-12-02 nginx環(huán)境配置首頁可以訪問,路由頁面都404報(bào)錯(cuò)
- 2022-04-25 利用Python寫個(gè)摸魚監(jiān)控進(jìn)程_python
- 2022-04-21 R語言繪制數(shù)據(jù)可視化Dumbbell?plot啞鈴圖_R語言
- 2022-11-17 Android復(fù)選框CheckBox與開關(guān)按鈕Switch及單選按鈕RadioButton使用示例詳
- 2023-03-16 Python?asyncio異步編程常見問題小結(jié)_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支