python add_argument()用法解析
這篇文章主要介紹了python add_argument()用法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
介紹:
argparse
argparse 是 Python 內(nèi)置的一個用于命令項選項與參數(shù)解析的模塊,通過在程序中定義好我們需要的參數(shù),argparse 將會從 sys.argv 中解析出這些參數(shù),并自動生成幫助和使用信息。當(dāng)然,Python 也有第三方的庫可用于命令行解析,而且功能也更加強(qiáng)大,比如 docopt,Click。
argparse 使用
簡單示例
我們先來看一個簡單示例。主要有三個步驟:
- 創(chuàng)建 ArgumentParser() 對象
- 調(diào)用 add_argument() 方法添加參數(shù)
- 使用 parse_args() 解析添加的參數(shù)
現(xiàn)在我們來簡單的測試一下:
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])
每個參數(shù)解釋如下:
- name or flags - 選項字符串的名字或者列表,例如 foo 或者 -f, --foo。
- action - 命令行遇到參數(shù)時的動作,默認(rèn)值是 store。
- store_const,表示賦值為const;
- append,將遇到的值存儲成列表,也就是如果參數(shù)重復(fù)則會保存多個值;
- append_const,將參數(shù)規(guī)范中定義的一個值保存到一個列表;
- count,存儲遇到的次數(shù);此外,也可以繼承 argparse.Action 自定義參數(shù)解析;
- nargs - 應(yīng)該讀取的命令行參數(shù)個數(shù),可以是具體的數(shù)字,或者是?號,當(dāng)不指定值時對于 Positional argument 使用 default,對于 Optional argument 使用 const;或者是 * 號,表示 0 或多個參數(shù);或者是 + 號表示 1 或多個參數(shù)。
- const - action 和 nargs 所需要的常量值。
- default - 不指定參數(shù)時的默認(rèn)值。
- type - 命令行參數(shù)應(yīng)該被轉(zhuǎn)換成的類型。
- choices - 參數(shù)可允許的值的一個容器。
- required - 可選參數(shù)是否可以省略 (僅針對可選參數(shù))。
- help - 參數(shù)的幫助信息,當(dāng)指定為 argparse.SUPPRESS 時表示不顯示該參數(shù)的幫助信息.
- metavar - 在 usage 說明中的參數(shù)名稱,對于必選參數(shù)默認(rèn)就是參數(shù)名稱,對于可選參數(shù)默認(rèn)是全大寫的參數(shù)名稱.
- dest - 解析后的參數(shù)名稱,默認(rèn)情況下,對于可選參數(shù)選取最長的名稱,中劃線轉(zhuǎn)換為下劃線.
然后對應(yīng)程序中的內(nèi)容:action - 命令行遇到參數(shù)時的動作,默認(rèn)值是 store。所以sparse返回的是 Ture,
以下同理:args.seed返回的是72,數(shù)據(jù)類型是int
args.epochs返回的是10000,數(shù)據(jù)類型是int
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 基于python中__add__函數(shù)的用法
- Python使用add_subplot與subplot畫子圖操作示例
- python中g(shù)etaddrinfo()基本用法實例分析
- python base64 decode incorrect padding錯誤解決方法
- Python socket.error: [Errno 98] Address already in use的原因和解決方法
- python構(gòu)造函數(shù)init實例方法解析
- python matplotlib中的subplot函數(shù)使用詳解
- Python實現(xiàn)計算長方形面積(帶參數(shù)函數(shù)demo)
- 解決python replace函數(shù)替換無效問題
相關(guān)文章
python使用PySimpleGUI設(shè)置進(jìn)度條及控件使用
PySimpleGUI是一個在tkinter基礎(chǔ)上的,足夠簡單,方便,pythonic的GUI庫.本文給大家介紹python使用PySimpleGUI設(shè)置進(jìn)度條的方法及進(jìn)度條控件使用代碼,感興趣的朋友跟隨小編一起看看吧2021-06-06解決運行django程序出錯問題 ''str''object has no attribute''_meta''
這篇文章主要介紹了解決運行django程序出錯問題 'str'object has no attribute'_meta',具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07使用python腳本自動創(chuàng)建pip.ini配置文件代碼實例
這篇文章主要介紹了使用python腳本自動創(chuàng)建pip.ini配置文件代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-09-09Tensorflow 自帶可視化Tensorboard使用方法(附項目代碼)
這篇文章主要介紹了Tensorflow 自帶可視化Tensorboard使用方法(附項目代碼),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02使用Python實現(xiàn)Mysql數(shù)據(jù)庫相關(guān)操作詳解
這篇文章主要介紹了使用Python實現(xiàn)Mysql數(shù)據(jù)庫相關(guān)操作詳解,pymysql是Python中操作數(shù)據(jù)庫的第三方模塊,通過這個模塊的相關(guān)方法,我們可以連接并且去操作mysql數(shù)據(jù)庫,需要的朋友可以參考下2023-08-08