1
votes

After I've learned about these 2 functions. Ive been wondering how to use the eval() inside the exec() function. I ask this didactically.

I tried this and other variations in the console but it was unsuccessful:

exec "eval("1+1")"
2

2 Answers

0
votes

Legal syntax would be exec("eval('1+1')") but that is pretty pointless, exec is for statements.

In [25]: exec("eval('1+1')")

In [26]: exec("print(1+1)")
2
In [27]: exec("a = eval('1+1')")
In [28]: a
Out[28]: 2
0
votes

You are using double-quotes to both surround the expression being execd and its argument, which confuses the parser; you can either escape the inner ones or use single-quotes for them.