Is it possible to include the operator in a variable when evaluating a condition. Like this:
>>> one = 1
>>> two = 2
>>> lessthan = '<'
>>> if one lessthan two: print 'yes'
is there a way to return the variable during the expression? I have also tried to return it via a function.
def operator(op):
return op
if one operator(lessthan) two: print 'yes'
Many thanks,
You could use
operator:You could also make a mapping between strings and operations:
And then call the function: