How can I turn a string such as "+"
into the operator plus?
Thanks!
-
1instead you can convert other things to string and evaluate stackoverflow.com/questions/729248/python-eval-and-globals – Xinus Nov 16 '09 at 8:03
Use a lookup table:
import operator
ops = { "+": operator.add, "-": operator.sub } # etc.
print ops["+"](1,1) # prints 2
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, get_operator_fn=ops.get):
op1, op2 = int(op1), int(op2)
return get_operator_fn(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())))
-
In python3, this gives: '+' : operator.add, AttributeError: 'str' object has no attribute 'add' – Juha Untinen May 6 '17 at 20:11
-
1The problem is that the poorly-named
operator
parameter clashes with the imported operator module. I'm guessing you inlinedget_operator_fn
witheval_binary_expr
. Change theoperator
parameter to something else, likeoper
, and all references to it. See my edit. – PaulMcG May 6 '17 at 20:22 -
4For Python3, you'll also have to change
operator.div
tooperator.truediv
, and fix all the print statements. – PaulMcG May 6 '17 at 20:23 -
1Note: This rebuilds the lookup
dict
from scratch on every call, removing theO(1)
lookup benefit. Define thedict
outside the function so it's only built once. – ShadowRanger Dec 20 '19 at 3:58 -
More thoroughly, @PaulMcG, Python3 drops operator.div for operator.truediv (returning a float) and operator.floordiv (Python2 style integer division) – Zim Feb 7 '20 at 21:24
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
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
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)
-
1this 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 – iratzhash Nov 22 '17 at 22:43
-
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)
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()