I tried
eval('print("hello world")') eval('return 0')
which are incorrect. Why are they invalid and what rules should be followed when using eval() (except as little as possible)?
eval()
In Python, eval() evaluates expressions (which results in a value). Both print and return defined as instructions (however, in Python 3, print is actually a function call, which is an expression). In the case of executing statements, you need the exec statement instead.
print
return
exec
eval () is used to evaluate the value of a variable as a variable.
Example:
var="Hello World!" code="var" print eval(code)
the conclusion should be:
Hello World!