I have to perform a certain arithmetic operation between decimal numbers. The operations can be 'add', 'subtract' or 'multiply', and the desired operation will be passed in to our function along with the numbers. The way I want to do this is that I want to store the ASCII values of the operations '+', '-' and '*' and then somehow use that to perform the operation between the two numbers.
def (a, b, op):
dict{'add':ord('+'), 'subtract':ord('-'), 'multiply':ord('*')}
return a dict[op] b # this statement does not work
Is there a way to achieve something like that in python ?
I know one alternative is to use:
from operator import add,sub,mul
and then use the imported values in the hash instead, but for this question I am interested in finding out if its possible to do it the way I asked in the original question. Thanks !