Python if-statement with variable mathematical operator

后端 未结 3 1377

I\'m trying to insert a variable mathematical operator into a if statement, an example of what I\'m trying to achieve in parsing user-supplied mathematical expressions:

3条回答
  •  [愿得一人]
    2020-12-18 21:25

    Use the operator module:

    import operator
    op = operator.eq
    
    if op("test", "test"):
       print "match found"
    

提交回复
热议问题