Explore Courses Blog Tutorials Interview Questions
0 votes
in Python by (47.6k points)

I've been looking at the dynamic evaluation of Python code, and come across the eval() and compile() functions, and the exec statement.

Can someone please explain the difference between eval and exec, and how the different modes of compile() fit in?

1 Answer

0 votes
by (106k points)

  1. The word exec is not an expression: When you work in Python 2.x it is a statement there, and in Python 3.x it is a function. Its work is to compile and evaluate the statement immediately.
  2. The word eval is a built-in function, it evaluates an expression and returns the value produced by that expression. 
  3. Whereas the word compile is a lower-level version of exec and eval. It returns a code object that can do it. 

Related questions

Browse Categories