Skip to main content
 首页 » 编程设计

python之将 0 和 1 字符串转换为 bool 值的最 Pythonic 方法是什么

2025年05月04日13lyj

<分区>

我正在从配置为输入的通用 IO (GPIO) 中读取一个值,它返回一个 0 或 1 的字符串。我看到了两种将其转换为 bool 值的简单方法:

bool(int(input_value)) 

或

not not int(input_value) 

哪个最 Pythonic?是否有比上面介绍的更多的 Pythonic 方式?