Use the operator package together with a dictionary to look up the operators according to their text equivalents. All of these must be either unary or binary operators to work consistently.
import operator
ops = {'==' : 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"
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…