I have tried
eval('print("hello world")')
eval('return 0')
which are both incorrect. Why are they invalid and what rules should I follow when using eval() (other than as little as possible)?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In Python,
eval()evaluates expressions (something that results in a value). Bothprintandreturnare defined as statements (however in Python 3,printis actually a function call, which is an expression). In the case of executing statements, you need to use theexecstatement instead.