python add_argument()用法解析
這篇文章主要介紹了python add_argument()用法解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
介紹:
argparse
argparse 是 Python 內(nèi)置的一個(gè)用于命令項(xiàng)選項(xiàng)與參數(shù)解析的模塊,通過在程序中定義好我們需要的參數(shù),argparse 將會(huì)從 sys.argv 中解析出這些參數(shù),并自動(dòng)生成幫助和使用信息。當(dāng)然,Python 也有第三方的庫(kù)可用于命令行解析,而且功能也更加強(qiáng)大,比如 docopt,Click。
argparse 使用
簡(jiǎn)單示例
我們先來(lái)看一個(gè)簡(jiǎn)單示例。主要有三個(gè)步驟:
- 創(chuàng)建 ArgumentParser() 對(duì)象
- 調(diào)用 add_argument() 方法添加參數(shù)
- 使用 parse_args() 解析添加的參數(shù)
現(xiàn)在我們來(lái)簡(jiǎn)單的測(cè)試一下:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--sparse', action='store_true', default=False, help='GAT with sparse version or not.')
parser.add_argument('--seed', type=int, default=72, help='Random seed.')
parser.add_argument('--epochs', type=int, default=10000, help='Number of epochs to train.')
args = parser.parse_args()
print(args.sparse)
print(args.seed)
print(args.epochs)
打印內(nèi)容如下:
/home/user/anaconda3/bin/python3.6 /home/user/lly/pyGAT-master/test.py False 72 10000 Process finished with exit code 0
舉例:
parser = argparse.ArgumentParser()
parser.add_argument('--sparse', action='store_true', help='GAT with sparse version or not.')
parser.add_argument('--seed', type=int, default=72, help='Random seed.')
parser.add_argument('--epochs', type=int, default=10000, help='Number of epochs to train.')
args = parser.parse_args()
print(args.sparse)
print(args.seed)
print(args.epochs)
打印如下:
False 72 10000
舉例
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--sparse', action='store_true', default=True, help='GAT with sparse version or not.')
parser.add_argument('--seed', type=int, default=72, help='Random seed.')
parser.add_argument('--epochs', type=int, default=10000, help='Number of epochs to train.')
args = parser.parse_args()
print(args.sparse)
print(args.seed)
print(args.epochs)
打印如下:
True
72
10000
先奉上add_argument() 方法定義如何解析命令行參數(shù):
ArgumentParser.add_argument(name or flags...[, action][, nargs][, const][, default][, type][, choices][, required][, help][, metavar][, dest])
每個(gè)參數(shù)解釋如下:
- name or flags - 選項(xiàng)字符串的名字或者列表,例如 foo 或者 -f, --foo。
- action - 命令行遇到參數(shù)時(shí)的動(dòng)作,默認(rèn)值是 store。
- store_const,表示賦值為const;
- append,將遇到的值存儲(chǔ)成列表,也就是如果參數(shù)重復(fù)則會(huì)保存多個(gè)值;
- append_const,將參數(shù)規(guī)范中定義的一個(gè)值保存到一個(gè)列表;
- count,存儲(chǔ)遇到的次數(shù);此外,也可以繼承 argparse.Action 自定義參數(shù)解析;
- nargs - 應(yīng)該讀取的命令行參數(shù)個(gè)數(shù),可以是具體的數(shù)字,或者是?號(hào),當(dāng)不指定值時(shí)對(duì)于 Positional argument 使用 default,對(duì)于 Optional argument 使用 const;或者是 * 號(hào),表示 0 或多個(gè)參數(shù);或者是 + 號(hào)表示 1 或多個(gè)參數(shù)。
- const - action 和 nargs 所需要的常量值。
- default - 不指定參數(shù)時(shí)的默認(rèn)值。
- type - 命令行參數(shù)應(yīng)該被轉(zhuǎn)換成的類型。
- choices - 參數(shù)可允許的值的一個(gè)容器。
- required - 可選參數(shù)是否可以省略 (僅針對(duì)可選參數(shù))。
- help - 參數(shù)的幫助信息,當(dāng)指定為 argparse.SUPPRESS 時(shí)表示不顯示該參數(shù)的幫助信息.
- metavar - 在 usage 說明中的參數(shù)名稱,對(duì)于必選參數(shù)默認(rèn)就是參數(shù)名稱,對(duì)于可選參數(shù)默認(rèn)是全大寫的參數(shù)名稱.
- dest - 解析后的參數(shù)名稱,默認(rèn)情況下,對(duì)于可選參數(shù)選取最長(zhǎng)的名稱,中劃線轉(zhuǎn)換為下劃線.
然后對(duì)應(yīng)程序中的內(nèi)容:action - 命令行遇到參數(shù)時(shí)的動(dòng)作,默認(rèn)值是 store。所以sparse返回的是 Ture,
以下同理:args.seed返回的是72,數(shù)據(jù)類型是int
args.epochs返回的是10000,數(shù)據(jù)類型是int
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 基于python中__add__函數(shù)的用法
- Python使用add_subplot與subplot畫子圖操作示例
- python中g(shù)etaddrinfo()基本用法實(shí)例分析
- python base64 decode incorrect padding錯(cuò)誤解決方法
- Python socket.error: [Errno 98] Address already in use的原因和解決方法
- python構(gòu)造函數(shù)init實(shí)例方法解析
- python matplotlib中的subplot函數(shù)使用詳解
- Python實(shí)現(xiàn)計(jì)算長(zhǎng)方形面積(帶參數(shù)函數(shù)demo)
- 解決python replace函數(shù)替換無(wú)效問題
相關(guān)文章
Python selenium抓取虎牙短視頻代碼實(shí)例
這篇文章主要介紹了Python selenium抓取虎牙短視頻代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
在django admin詳情表單顯示中添加自定義控件的實(shí)現(xiàn)
這篇文章主要介紹了在django admin詳情表單顯示中添加自定義控件的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-03-03
python數(shù)據(jù)分析基礎(chǔ)知識(shí)之shape()函數(shù)的使用教程
shape函數(shù)是numpy.core.fromnumeric中的函數(shù),它的功能是讀取矩陣的長(zhǎng)度,比如shape[0]就是讀取矩陣第一維度的長(zhǎng)度,下面這篇文章主要給大家介紹了關(guān)于python數(shù)據(jù)分析基礎(chǔ)知識(shí)之shape()函數(shù)使用的相關(guān)資料,需要的朋友可以參考下2022-09-09
pytorch實(shí)現(xiàn)對(duì)輸入超過三通道的數(shù)據(jù)進(jìn)行訓(xùn)練
今天小編就為大家分享一篇pytorch實(shí)現(xiàn)對(duì)輸入超過三通道的數(shù)據(jù)進(jìn)行訓(xùn)練,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-01-01
全面分析Python的優(yōu)點(diǎn)和缺點(diǎn)
本篇文章給大家詳細(xì)分析了Python的優(yōu)點(diǎn)和缺點(diǎn)以及相關(guān)的優(yōu)勢(shì)劣勢(shì)分析,對(duì)此有興趣的朋友學(xué)習(xí)下。2018-02-02

