views:

661

answers:

1

I've been looking at 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?

+9  A: 
  1. exec is a statement, not an expression (at least in Python 2.x, maybe not in 3.x?). It compiles and immediately evaluates a statement or set of statement contained in a string. Example:

    exec 'print 5'           # prints 5.
    exec 'print 5\nprint 6'  # prints 5{newline}6.
    exec 'if True: print 6'  # prints 6.
    exec '5'                 # does nothing and returns nothing.
    
  2. eval evaluates an expression (not a statement) and returns the value that expression produces. Example:

    x = eval('5')              # x <- 5
    x = eval('%d + 6' % x)     # x <- 11
    x = eval('abs(%d)' % -100) # x <- 100
    x = eval('print 5')        # INVALID; print is a statement, not an expression (in Python 2.x).
    x = eval('if 1: x = 4')    # INVALID; if is a statement, not an expression.
    
  3. compile is a lower level version of exec and eval. It does not execute or evaluate your statements or expressions, but returns a code object that can do it. The modes are as follows:

    1. compile(string, '', 'eval') returns the code object that would have been executed had you done eval(string). Note that you cannot use statements in this mode; only a (single) expression is valid.
    2. compile(string, '', 'exec') returns the code object that would have been executed had you done exec(string). You can use any number of statements here.
    3. compile(string, '', 'single') is like the exec mode, but it will ignore everything except for the first statement. Note that an if/else statement with its results is considered a single statement.
Max Shawabkeh
In Python 3, `exec()` now is in fact a function.
Tim Pietzcker
Since (as you point out), `exec` is a statement in the version you were targeting, it's deceptive to include those parens, and if you try to use the `in globals, locals`, also buggy.
Mike Graham