What are the valid statements inside python eval ()?

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)?

+4
source share
2 answers

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.

+8
source

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! 
0
source

All Articles