最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python編程argparse入門淺析

 更新時(shí)間:2018年02月07日 16:51:26   作者:foryouslgme  
這篇文章主要介紹了Python編程argparse入門淺析,分享了相關(guān)代碼,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下

本文研究的主要是Python編程argparse的相關(guān)內(nèi)容,具體介紹如下。

#aaa.py
#version 3.5
import os    #這句是沒(méi)用了,不知道為什么markdown在編輯代碼時(shí),不加這一句,就不能顯示代碼高亮[汗]
import argparse


parser = argparse.ArgumentParser(description='Process some integers...')  #初始化一個(gè)分析器
#parser.add_argument(中的參數(shù))
#__init__(self, option_strings, dest, nargs=None, const=None, default=None, type=None, choices=None, required=False, help=None, metavar=None)
parser.add_argument('integers',metavar='N',type=int,nargs='+',
          help='an integer for the accumulator')    
          #這是一個(gè)添加【位置參數(shù)】
          #第一個(gè)參數(shù)是自定義的參數(shù)名,在代碼中用來(lái)計(jì)算的(parser.parse_args().integers*2)


parser.add_argument('--sum',dest='accumulate',action='store_const',
          const=sum,default=max,
          help='sum the integers(default:find the max)')
          #這是一個(gè)添加【可選參數(shù)】
          #第一個(gè)參數(shù)是自定義的參數(shù)【在代碼中的使用parser.parse_args().sum】【在系統(tǒng)命令行中的使用:>python aaa.py --sum



args = parser.parse_args()
print(args)       #Namespace(accumulate=<built-in function sum>, integers2=[1, 2, 3, 4])
print(args.integers)  #integers要與上面的對(duì)應(yīng)
print(args.accumulate(args.integers))  #accumulate要與上面的對(duì)應(yīng)

在系統(tǒng)命令行中進(jìn)行參數(shù)調(diào)用結(jié)果如下:

D:\Program Files (x86)\Python35>python aaa.py -h
usage: aaa.py [-h] [--sum] N [N ...]

Process some integers...

positional arguments:
N an integer for the accumulator

optional arguments:
-h, --help show this help message and exit
--sum sum the integers(default:find the max)


D:\Program Files (x86)\Python35>python aaa.py 1 2 3 4 --sum
Namespace(accumulate=<built-in function sum>, integers2=[1, 2, 3, 4])
[1, 2, 3, 4]
10

D:\Program Files (x86)\Python35>python aaa.py 1 2 3 4
Namespace(accumulate=<built-in function max>, integers2=[1,2,3,4])
[1, 2, 3, 4]
4

在python交互模式下運(yùn)行結(jié)果如下:

附件

Keyword Arguments:
|
| - option_strings -- A list of command-line option strings which
| should be associated with this action.
|
| - dest -- The name of the attribute to hold the created object(s)
|
| - nargs -- The number of command-line arguments that should be
| consumed. By default, one argument will be consumed and a single
| value will be produced. Other values include:
| - N (an integer) consumes N arguments (and produces a list)
| - '?' consumes zero or one arguments
| - '*' consumes zero or more arguments (and produces a list)
| - '+' consumes one or more arguments (and produces a list)
| Note that the difference between the default and nargs=1 is that
| with the default, a single value will be produced, while with
| nargs=1, a list containing a single value will be produced.
|
| - const -- The value to be produced if the option is specified and the
| option uses an action that takes no values.
|
| - default -- The value to be produced if the option is not specified.
|
| - type -- A callable that accepts a single string argument, and
| returns the converted value. The standard Python types str, int,
| float, and complex are useful examples of such callables. If None,
| str is used.
|
| - choices -- A container of values that should be allowed. If not None,
| after a command-line argument has been converted to the appropriate
| type, an exception will be raised if it is not a member of this
| collection.
|
| - required -- True if the action must always be specified at the
| command line. This is only meaningful for optional command-line
| arguments.
|
| - help -- The help string describing the argument.
|
| - metavar -- The name to be used for the option's argument with the
| help string. If None, the 'dest' value will be used as the name.

總結(jié)

以上就是本文關(guān)于Python編程argparse入門淺析的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!

相關(guān)文章

  • ChatGPT 幫我自動(dòng)編寫(xiě) Python 爬蟲(chóng)腳本的詳細(xì)過(guò)程

    ChatGPT 幫我自動(dòng)編寫(xiě) Python 爬蟲(chóng)腳本的詳細(xì)過(guò)程

    ChatGPT是一種基于大語(yǔ)言模型的生成式AI,換句話說(shuō)它可以自動(dòng)生成類似人類語(yǔ)言的文本,把梳理好的有邏輯的答案呈現(xiàn)在你面前,這完全不同于傳統(tǒng)搜索工具,這篇文章主要介紹了ChatGPT 幫我自動(dòng)編寫(xiě) Python 爬蟲(chóng)腳本,需要的朋友可以參考下
    2023-02-02
  • pycharm 關(guān)掉syntax檢查操作

    pycharm 關(guān)掉syntax檢查操作

    這篇文章主要介紹了pycharm 關(guān)掉syntax檢查操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-06-06
  • python獲取當(dāng)前目錄路徑和上級(jí)路徑的實(shí)例

    python獲取當(dāng)前目錄路徑和上級(jí)路徑的實(shí)例

    下面小編就為大家分享一篇python獲取當(dāng)前目錄路徑和上級(jí)路徑的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • python實(shí)現(xiàn)畫(huà)五角星和螺旋線的示例

    python實(shí)現(xiàn)畫(huà)五角星和螺旋線的示例

    今天小編就為大家分享一篇python實(shí)現(xiàn)畫(huà)五角星和螺旋線的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-01-01
  • Python打工人必備之windows倒計(jì)時(shí)鎖屏功能的實(shí)現(xiàn)

    Python打工人必備之windows倒計(jì)時(shí)鎖屏功能的實(shí)現(xiàn)

    每個(gè)人的電腦里都會(huì)有不想讓別人知道的隱私,或者是上班時(shí)間偷偷摸魚(yú)怕被發(fā)現(xiàn)的小秘密。那怎么辦?就干脆把隱私鎖起來(lái)!從源頭上杜絕被他人偷窺自己的隱私。本文就來(lái)用Python實(shí)現(xiàn)一個(gè)windows倒計(jì)時(shí)鎖屏功能,需要的可以參考一下
    2023-04-04
  • Python設(shè)計(jì)模式行為型責(zé)任鏈模式

    Python設(shè)計(jì)模式行為型責(zé)任鏈模式

    這篇文章主要介紹了Python設(shè)計(jì)模式行為型責(zé)任鏈模式,責(zé)任鏈模式將能處理請(qǐng)求的對(duì)象連成一條鏈,并沿著這條鏈傳遞該請(qǐng)求,直到有一個(gè)對(duì)象處理請(qǐng)求為止,避免請(qǐng)求的發(fā)送者和接收者之間的耦合關(guān)系,下圍繞改內(nèi)容介紹具有一點(diǎn)的參考價(jià)值,需要的朋友可以參考下
    2022-02-02
  • 實(shí)操Python爬取覓知網(wǎng)素材圖片示例

    實(shí)操Python爬取覓知網(wǎng)素材圖片示例

    大家好,本篇文章介紹的是實(shí)操Python爬取覓知網(wǎng)素材圖片示例,感興趣的朋友趕快來(lái)看一看吧,對(duì)你有用的話記得收藏起來(lái),方便下次瀏覽
    2021-11-11
  • 簡(jiǎn)單談?wù)凱ython中的json與pickle

    簡(jiǎn)單談?wù)凱ython中的json與pickle

    下面小編就為大家?guī)?lái)一篇簡(jiǎn)單談?wù)凱ython中的json與pickle。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-07-07
  • python 網(wǎng)絡(luò)編程詳解及簡(jiǎn)單實(shí)例

    python 網(wǎng)絡(luò)編程詳解及簡(jiǎn)單實(shí)例

    這篇文章主要介紹了python 網(wǎng)絡(luò)編程詳解及簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • Python監(jiān)控主機(jī)是否存活并以郵件報(bào)警

    Python監(jiān)控主機(jī)是否存活并以郵件報(bào)警

    本文是利用python腳本寫(xiě)的簡(jiǎn)單測(cè)試主機(jī)是否存活,此腳本有個(gè)缺點(diǎn)不適用線上,由于網(wǎng)絡(luò)延遲、丟包現(xiàn)象會(huì)造成誤報(bào)郵件,感興趣的朋友一起看看Python監(jiān)控主機(jī)是否存活并以郵件報(bào)警吧
    2015-09-09

最新評(píng)論

宁南县| 海兴县| 高唐县| 滕州市| 神池县| 禹州市| 磴口县| 华安县| 惠来县| 精河县| 唐海县| 赤城县| 定远县| 新干县| 广丰县| 渑池县| 南乐县| 虞城县| 泰州市| 台中市| 兰州市| 当涂县| 军事| 新野县| 准格尔旗| 嘉荫县| 同德县| 大英县| 河间市| 雅安市| 中牟县| 登封市| 霍城县| 玛沁县| 中卫市| 那曲县| 德令哈市| 安平县| 农安县| 大足县| 临汾市|