Pyhton: eval(), exec() or another function?
Clash Royale CLAN TAG#URR8PPP
Pyhton: eval(), exec() or another function?
I have a problem. What should I use? Here's the code:
import example
a = 1
b = "+"
c = 3
g = example.Calc(a, b, c)
result = example.Calc.f
print(f"The result is result")
example.py:
class Calc:
f = None
def __init__(self, a, b, c):
eval(f"d = a b c")
Calc.f = d
It should print "The result is 4", but instead it gives this error:
Traceback (most recent call last):
File "C:(Path to script)example.py", line 4, in __init__
eval(f"d = a b c")
File "<string>", line 1
d = a - c
^
SyntaxError: invalid syntax
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.