当前位置:网站首页>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。
边栏推荐
猜你喜欢

如何高效开发一款微信小程序

sqli-labs通關匯總-page2

Ceaspectuss shipping company shipping artificial intelligence products, anytime, anywhere container inspection and reporting to achieve cloud yard, shipping company intelligent digital container contr

sqli-labs通关汇总-page1

Build FRP for intranet penetration

MySQL无order by的排序规则因素

Review of reflection topics

在php的开发环境中如何调取WebService?

ORACLE EBS中消息队列fnd_msg_pub、fnd_message在PL/SQL中的应用

SQLI-LABS通关(less1)
随机推荐
Oracle apex Ajax process + dy verification
Module not found: Error: Can't resolve './$$_gendir/app/app.module.ngfactory'
ORACLE 11G利用 ORDS+pljson来实现json_table 效果
2021-07-17c /cad secondary development creation circle (5)
Oracle 11g uses ords+pljson to implement JSON_ Table effect
Cloud picture says | distributed transaction management DTM: the little helper behind "buy buy buy"
Sqli-labs customs clearance (less1)
2021-07-05C#/CAD二次开发创建圆弧(4)
@Transational踩坑
Oracle EBS interface development - quick generation of JSON format data
MySQL组合索引加不加ID
Oracle general ledger balance table GL for foreign currency bookkeeping_ Balance change (Part 1)
Explain in detail the process of realizing Chinese text classification by CNN
Ingress Controller 0.47.0的Yaml文件
php中的二维数组去重
PHP Session原理简析
UEditor . Net version arbitrary file upload vulnerability recurrence
sqli-labs通关汇总-page4
Oracle 11g sysaux table space full processing and the difference between move and shrink
Message queue fnd in Oracle EBS_ msg_ pub、fnd_ Application of message in pl/sql