当前位置:网站首页>parser.parse_args 布尔值类型将False解析为True
parser.parse_args 布尔值类型将False解析为True
2022-07-02 06:25:00 【lwgkzl】
记录一个大坑,遇到好多次了,一直没长记性 ==!
问题
当我在代码中使用parser添加一个参数的时候, 将其设置成bool类型的,然后默认值为True。
parser.add_argument("--some_argument", default=True, type=bool)然而,当我在命令行中输入 --some_argument=False的时候, 代码跑出来some_argument这个参数还是True。
我怀疑是自己的编码姿势不对, 换了--some_argument=false(小写) --some_argument=0等,结果代码跑的时候some_arguemnt仍然是True值。
原因
在命令行中,输入的false,或者“0”或者“False”, 都被解析成了字符串,而字符串转成布尔类型之后是这样的:
>>> bool("false")
True
>>> bool("False")
True
>>> bool("0")
True
所以无论我们在命令行里面输入什么, 代码里面解析出来都是True。
解决方案
1. 简单有效, 从此不设置默认值为True的参数, 只设置默认值为False的参数。那么一般跑实验他默认就是False, 如果需要使得该参数的值变为True, 则只需要在命令行输入该参数=True就行了,这样该参数会被解析为True.
parser.add_argument("--some_argument", default=False, type=bool)2. 第二种就是使用 “store_true”这个设置, 默认效果和上面基本一致
parser.add_argument("--some_argument", action="store_true")如果想让该参数为True, 则在命令行中输入--some_argument, 不输入则默认为False。
边栏推荐
猜你喜欢

PHP Session原理简析

DNS攻击详解

Oracle apex Ajax process + dy verification

Two table Association of pyspark in idea2020 (field names are the same)

类加载器及双亲委派机制

Oracle 11g uses ords+pljson to implement JSON_ Table effect

SQLI-LABS通关(less18-less20)

UEditor . Net version arbitrary file upload vulnerability recurrence

Sqli - Labs Clearance (less6 - less14)

離線數倉和bi開發的實踐和思考
随机推荐
oracle-外币记账时总账余额表gl_balance变化(上)
Oracle EBS数据库监控-Zabbix+zabbix-agent2+orabbix
PXC high availability cluster summary
sqli-labs通关汇总-page2
Uniapp introduces local fonts
数仓模型事实表模型设计
User login function: simple but difficult
The boss said: whoever wants to use double to define the amount of goods, just pack up and go
JS to determine whether there is a value in the object in the array
类加载器及双亲委派机制
Sqli - Labs Clearance (less6 - less14)
UEditor .Net版本任意文件上传漏洞复现
2021-07-17c /cad secondary development creation circle (5)
第一个快应用(quickapp)demo
ORACLE 11.2.0.3 不停机处理SYSAUX表空间一直增长问题
Illustration of etcd access in kubernetes
php中在二维数组中根据值返回对应的键值
SSM二手交易网站
Oracle rman自动恢复脚本(生产数据向测试迁移)
Oracle EBS interface development - quick generation of JSON format data