【发布时间】:2018-12-04 18:01:55
【问题描述】:
如何在 Python 中将字符串转换为逻辑参数,作为if() 函数的参数插入?
例如:
string = "(A != 0) and (B >= 5)"
如何将这个字符串转换成它所代表的逻辑,以便插入到if() 函数中?
【问题讨论】:
标签: string python-3.x if-statement logic logical-operators
如何在 Python 中将字符串转换为逻辑参数,作为if() 函数的参数插入?
例如:
string = "(A != 0) and (B >= 5)"
如何将这个字符串转换成它所代表的逻辑,以便插入到if() 函数中?
【问题讨论】:
标签: string python-3.x if-statement logic logical-operators
您可以使用 eval 函数来评估字符串:
string = "(A != 0) and (B >= 5)"
if eval(string):
doSomething()
【讨论】: