Use A String (representing A Logical Operator) In A Python Expression
Is it possible to somehow cast a string of, say, or or and into a form that is recognizable as a logical operator? For example, is it possible to do something like this: l = [1, 2,
Solution 1:
You may use the operator
package:
importoperator
o = {item1: operator.or_}
if o[item1](i>4, i<0):
...
Note that or_
does not short-circuit, like or
does. If you really need the short-circuit behaviour, you can use eval
(but this is in general not recommended).
Post a Comment for "Use A String (representing A Logical Operator) In A Python Expression"