当前位置:网站首页>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。
边栏推荐
- ARP攻击
- 一个中年程序员学习中国近代史的小结
- JS create a custom JSON array
- JSP智能小区物业管理系统
- Only the background of famous universities and factories can programmers have a way out? Netizen: two, big factory background is OK
- php中在二维数组中根据值返回对应的键值
- 叮咚,Redis OM对象映射框架来了
- 图解Kubernetes中的etcd的访问
- Sqli-labs customs clearance (less6-less14)
- ARP attack
猜你喜欢
TCP攻击
2021-07-05c /cad secondary development create arc (4)
Review of reflection topics
Analysis of MapReduce and yarn principles
mapreduce概念和案例(尚硅谷学习笔记)
Oracle EBs and apex integrated login and principle analysis
sqli-labs通关汇总-page4
Build FRP for intranet penetration
Sqli-labs customs clearance (less1)
UEditor .Net版本任意文件上传漏洞复现
随机推荐
Oracle 11g sysaux table space full processing and the difference between move and shrink
php中通过集合collect的方法来实现把某个值插入到数组中指定的位置
DNS attack details
华为机试题
view的绘制机制(三)
Oracle rman自动恢复脚本(生产数据向测试迁移)
Explain in detail the process of realizing Chinese text classification by CNN
Ingress Controller 0.47.0的Yaml文件
ORACLE EBS DATAGUARD 搭建
php中生成随机的6位邀请码
TCP attack
Ceaspectuss shipping company shipping artificial intelligence products, anytime, anywhere container inspection and reporting to achieve cloud yard, shipping company intelligent digital container contr
叮咚,Redis OM对象映射框架来了
数仓模型事实表模型设计
ssm超市订单管理系统
Oracle apex 21.2 installation and one click deployment
JS delete the last bit of the string
SQL注入闭合判断
MapReduce与YARN原理解析
Two table Association of pyspark in idea2020 (field names are the same)