Skip to main content
 首页 » 编程设计

python之if 语句中的几个参数

2025年05月04日12zhoujg

我有一个依赖于 argparse 的脚本。脚本的主体有这样的 if 语句:

if results.short == True and results.verbose == False and results.verbose2 == False and results.list == False and results.true == False: 

有没有更短的方法来做到这一点?假设我有超过这 5 个参数,在每个语句中输入每个参数似乎是重复性工作。

不能做类似的事情:

if results.short == True and "results.%s"== False % (everyotherresults.something): 

我正在为 Python 2.7 编写

请您参考如下方法:

你不应该在 bool 表达式中与 bool 进行比较,例如:

if (results.short  
    and not results.verbose  
    and not results.verbose2  
    and not results.list  
    and not results.true):