Alex Rivera | Logout

What does Python's eval() do?

Asked 2012-02-21T19:19:16.700
348

The book that I am reading on Python repeatedly shows code like eval(input('blah')).

How exactly does this modify the result from calling input?


See also: Why is using 'eval' a bad practice? to understand the critical security risks created by using eval or exec on untrusted input (i.e.: anything that is even partially under the user's control, rather than the program's control).

See also: How can I sandbox Python in pure Python?. The short version is that doing this properly will always be harder than choosing a proper tool instead of eval or exec.

See Using python's eval() vs. ast.literal_eval() for a potentially safer technique.

See How do I use raw_input in Python 3? for background context on why a book might have contained code like this, or why OP might originally have expected an input result not to require further processing.

Edit
Report

1 Answer

6

Maybe a misleading example of reading a line and interpreting it.

Try eval(input()) and type "1+1" - this should print 2. Eval evaluates expressions.

answered 2012-02-21T19:27:32.380

Your Answer