Use a string (representing a logical operator) in a Python expression -
is possible somehow cast string of, say, or or and form recognizable logical operator?
for example, possible this:
l = [1, 2, 3, 4, 5] o = {item1:'or'} in l: if > 4 o[item1] < 0: print where o[item1] recognized valid or logical operator?
you may use operator package:
import operator o = {item1: operator.or_} if o[item1](i>4, i<0): ... note or_ not short-circuit, or does. if need short-circuit behaviour, can use eval (but in general not recommended).
Comments
Post a Comment