2": print "1 is greater than 2" 但是被解析为 if 1 > 2: print -6ren">
gpt4 book ai didi

python - 将字符串解析为 boolean 值?

转载 作者:行者123 更新时间:2023-11-30 22:51:39 25 4
gpt4 key购买 nike

是否可以将 if 语句中的字符串解析为字符串?类似的东西

if "1 > 2":
print "1 is greater than 2"

但是被解析为

if 1 > 2:
print "1 is greater than 2"

这可能吗?有一个程序可以做到这一点吗?

最佳答案

这就是eval是为了。

if eval("1 > 2"):
print "1 is greater than 2"

小心 eval , 尽管。它将调用提供给它的任何函数。喜欢os.system('rm -rf /') :/

关于python - 将字符串解析为 boolean 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38948483/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com