95

How can I turn a string such as "+" into the operator plus?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
hwong557
  • 1,309
  • 1
  • 10
  • 15
  • 2
    instead you can convert other things to string and evaluate http://stackoverflow.com/questions/729248/python-eval-and-globals – Xinus Nov 16 '09 at 08:03

8 Answers8

151

Use a lookup table:

import operator
ops = { "+": operator.add, "-": operator.sub } # etc.

print(ops["+"](1,1)) # prints 2 
PaulMcG
  • 62,419
  • 16
  • 94
  • 130
Amnon
  • 7,652
  • 2
  • 26
  • 34
42
import operator

ops = {
    '+' : operator.add,
    '-' : operator.sub,
    '*' : operator.mul,
    '/' : operator.truediv,  # use operator.div for Python 2
    '%' : operator.mod,
    '^' : operator.xor,
}

def eval_binary_expr(op1, oper, op2):
    op1, op2 = int(op1), int(op2)
    return ops[oper](op1, op2)

print(eval_binary_expr(*("1 + 3".split())))
print(eval_binary_expr(*("1 * 3".split())))
print(eval_binary_expr(*("1 % 3".split())))
print(eval_binary_expr(*("1 ^ 3".split())))
PaulMcG
  • 62,419
  • 16
  • 94
  • 130
  • In python3, this gives: '+' : operator.add, AttributeError: 'str' object has no attribute 'add' – Juha Untinen May 06 '17 at 20:11
  • 1
    The problem is that the poorly-named `operator` parameter clashes with the imported operator module. I'm guessing you inlined `get_operator_fn` with `eval_binary_expr`. Change the `operator` parameter to something else, like `oper`, and all references to it. See my edit. – PaulMcG May 06 '17 at 20:22
  • 4
    For Python3, you'll also have to change `operator.div` to `operator.truediv`, and fix all the print statements. – PaulMcG May 06 '17 at 20:23
  • 1
    Note: This rebuilds the lookup `dict` from scratch on every call, removing the `O(1)` lookup benefit. Define the `dict` outside the function so it's only built once. – ShadowRanger Dec 20 '19 at 03:58
  • 1
    More thoroughly, @PaulMcG, Python3 drops operator.div for operator.truediv (returning a float) and operator.floordiv (Python2 style integer division) – Zim Feb 07 '20 at 21:24
  • For a full manual, visit https://docs.python.org/3/library/operator.html – NixRam Apr 20 '20 at 12:18
  • for exponent you want '**' : operator.pow – Brisco Oct 08 '21 at 21:03
17

How about using a lookup dict, but with lambdas instead of operator library.

op = {'+': lambda x, y: x + y,
      '-': lambda x, y: x - y}

Then you can do:

print(op['+'](1,2))

And it will output:

3
Garrett
  • 171
  • 1
  • 3
11

You can try using eval(), but it's dangerous if the strings are not coming from you. Else you might consider creating a dictionary:

ops = {"+": (lambda x,y: x+y), "-": (lambda x,y: x-y)}

etc... and then calling

ops['+'] (1,2)
or, for user input:
if ops.haskey(userop):
    val = ops[userop](userx,usery)
else:
    pass #something about wrong operator
Krzysztof Bujniewicz
  • 2,407
  • 21
  • 16
6

There is a magic method corresponding to every operator

OPERATORS = {'+': 'add', '-': 'sub', '*': 'mul', '/': 'div'}

def apply_operator(a, op, b):

    method = '__%s__' % OPERATORS[op]
    return getattr(b, method)(a)

apply_operator(1, '+', 2)
Vinayak Kaniyarakkal
  • 1,110
  • 17
  • 23
  • 1
    this works well, thanks for sharing. for validation we can extend apply_operator. if op == '/' and b == 0: return np.inf if op == '/' and a == 0: return 0 – Irtaza Nov 22 '17 at 22:43
  • This doesn't let [NotImplemented](https://docs.python.org/3/library/constants.html#NotImplemented) work properly. – wim Jan 25 '18 at 20:20
2

Use eval() if it is safe (not on servers, etc):

num_1 = 5

num_2 = 10

op = ['+', '-', '*']

result = eval(f'{num_1} {op[0]} {num_2}')

print(result)

Output : 15

Reza Rahemtola
  • 1,182
  • 7
  • 16
  • 30
Kamelia
  • 41
  • 4
1

I understand that you want to do something like: 5"+"7 where all 3 things would be passed by variables, so example:

import operator

#define operators you wanna use
allowed_operators={
    "+": operator.add,
    "-": operator.sub,
    "*": operator.mul,
    "/": operator.truediv}

#sample variables
a=5
b=7
string_operator="+"

#sample calculation => a+b
result=allowed_operators[string_operator](a,b)
print(result)
0

I was bugged with the same problem, using Jupyter Notebook, I was unable to import the operator module. So the above code helped give me insight but was unable to run on the platform. I figured out a somehwhat primitive way to do so with all the basic funcs and here it is: (This could be heavily refined but it’s a start…)

# Define Calculator and fill with input variables
# This example "will not" run if aplha character is use for num1/num2 
def calculate_me():
    num1 = input("1st number: ")
    oper = input("* OR / OR + OR - : ")
    num2 = input("2nd number: ")

    add2 = int(num1) + int(num2)
    mult2 = int(num1) * int(num2)
    divd2 = int(num1) / int(num2)
    sub2 = int(num1) - int(num2)

# Comparare operator strings 
# If input is correct, evaluate operand variables based on operator
    if num1.isdigit() and num2.isdigit():
        if oper is not "*" or "/" or "+" or "-":
            print("No strings or ints for the operator")
        else:
            pass
        if oper is "*":
            print(mult2)
        elif oper is "/":
            print(divd2)
        elif oper is "+":
            print(add2)
        elif oper is "-":
            print(sub2)
        else:
            return print("Try again")

# Call the function
calculate_me()
print()
calculate_me()
print()