惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
P
Proofpoint News Feed
宝玉的分享
宝玉的分享
人人都是产品经理
人人都是产品经理
博客园_首页
爱范儿
爱范儿
博客园 - 叶小钗
aimingoo的专栏
aimingoo的专栏
S
SegmentFault 最新的问题
MyScale Blog
MyScale Blog
阮一峰的网络日志
阮一峰的网络日志
IT之家
IT之家
Microsoft Security Blog
Microsoft Security Blog
Blog — PlanetScale
Blog — PlanetScale
博客园 - 【当耐特】
Y
Y Combinator Blog
量子位
博客园 - 三生石上(FineUI控件)
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
The Blog of Author Tim Ferriss
月光博客
月光博客
有赞技术团队
有赞技术团队
Apple Machine Learning Research
Apple Machine Learning Research
A
About on SuperTechFans

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
Set up the Default Value for Boolean Option in Argparse
2018-10-11 · via jdhao's digital space

TL;DR

If you want to set a parameter’s default value to True using argparse, use

parser.add_argument('--param', action='store_false')

Otherwise, use

parser.add_argument('--param', action='store_true')

when we use argparse to manager program parameters and add a parameter, we often use the action options to set the parameter’s value to a boolean value. But I have previously misunderstood this options and made a mistake. In this post, I will explain how does this option work and how to use it correctly.

Typically we use action='store_true' or action='store_false' to set the parameter to boolean value. Previously, I thought that action='store_true' will set the parameter value to True and action='store_false' will set the parameter value to False. In fact, it is not true. Suppose we have a script toy_parser.py:

import argparase

parser = argparse.ArgumentParser(description="A toy argument parser")
parser.add_argument('--param1', action='store_true')
parser.add_argument('--param2', action='store_false')

args = parser.parse_args()

print(args)

If we run this script on the command line with no argument

the output will be:

Namespace(param1=False, param2=True)

The result is exactly contratry to what we thought.

What about run the script with parameters?

$ python toy_parser.py –param1 –param2 Namespace(param1=True, param2=False)

$ python toy_parser.py –param1 Namespace(param1=True, param2=True)

$ python toy_parser.py –param2 Namespace(param1=False, param2=False)

In summary, if a parameter has action store_true but is missing in the command line, the default value will be False, otherwise, the value will be True. Similarly, if a parameter has action store_false but is missing in the command line, the default value will be True, otherwise, the value will be False.

The following two statements are problematic:

parser.add_argument('--param1', action='store_true', default=True)
parser.add_argument('--param2', action='store_false', default=False)

Wheter you use these paramters on the command line, they will always be True or False, which is not what we want.