Turn string into operator
evaluation, python
Solution
Use a lookup table:
import operator
ops = { "+": operator.add, "-": operator.sub } # etc.
print(ops["+"](1,1)) # prints 2
Problem
How can I turn a string such as `"+"` into the operator plus?
evaluation, python
Use a lookup table:
import operator
ops = { "+": operator.add, "-": operator.sub } # etc.
print(ops["+"](1,1)) # prints 2
How can I turn a string such as `"+"` into the operator plus?