My question is about how to make the compiler treats my string as an executable statement, consider my string mystr='1+2+3', now the question is what's the code to put in function func
so when I execute the following command func(mystr)
I get 6 on my screen.
Asked
Active
Viewed 78 times
-3

Mehdi
- 41
- 9
-
1use `eval(mystr)` – Ghilas BELHADJ Sep 01 '17 at 15:20
-
Solved, Thank you. – Mehdi Sep 01 '17 at 15:22
3 Answers
3
Using exec() for more than just expression strings:
def func(mystr):
exec(mystr)
func("mystr='1+2+3';print(mystr)")
-
-
@franciscosollima [exec()](https://docs.python.org/3/library/functions.html#exec) take any Python code (sentences) while [eval()](https://docs.python.org/3/library/functions.html#eval) just expressions. The doc linked is clear ;) – Sep 01 '17 at 15:28