结合使用运算符包和字典来根据其等效文本查找运算符。所有这些都必须是一元或二进制运算符,才能保持一致。
import operatorops = {'==' : operator.eq, '!=' : operator.ne, '<=' : operator.le, '>=' : operator.ge, '>' : operator.gt, '<' : operator.lt}maths_operator = "=="if ops[maths_operator]("test", "test"): print "match found"maths_operator = "!="if ops[maths_operator]("test", "test"): print "match found"else: print "match not found"


