eval
exec
compile
Python programming
code execution

What's the difference between eval, exec, and compile?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

eval, exec, and compile all deal with dynamic Python code, but they solve different problems. eval evaluates a single expression and returns a value, exec runs Python statements for their side effects, and compile turns source text into a reusable code object.

They are related, but not interchangeable. If you mix them up, you usually get syntax errors, missing return values, or code that is much harder to reason about than it needs to be.

Use eval for expressions that return a value

eval accepts a Python expression, not a block of statements. Because an expression produces a value, eval returns that value.

python
result = eval("2 + 3 * 4")
print(result)  # 14

It also works with a provided variable scope:

python
context = {"price": 20, "tax": 1.13}
total = eval("price * tax", {}, context)
print(total)  # 22.6

What eval cannot do is run statements such as assignments, loops, or function definitions:

python
eval("x = 10")  # SyntaxError

That is because x = 10 is a statement, not an expression.

Use exec for statements and code blocks

exec runs Python code for its effects. It does not return the result of the last line.

python
1namespace = {}
2
3exec(
4    """
5value = 10
6double = value * 2
7""",
8    {},
9    namespace,
10)
11
12print(namespace["double"])  # 20

This makes exec suitable for dynamic function definitions, assignments, imports, or multi-line code blocks.

python
1namespace = {}
2
3exec(
4    """
5def greet(name):
6    return f"Hello, {name}"
7""",
8    {},
9    namespace,
10)
11
12print(namespace["greet"]("Ada"))

Because exec is statement-oriented, it is more flexible than eval, but it is also harder to contain safely.

Use compile when you want to parse once and run later

compile does not execute code by itself. It converts source code into a code object that can later be passed to eval or exec.

python
code = compile("3 * (7 + 1)", "<expression>", "eval")
print(eval(code))  # 24

For statements:

python
1code = compile(
2    """
3total = 0
4for number in range(5):
5    total += number
6""",
7    "<script>",
8    "exec",
9)
10
11scope = {}
12exec(code, {}, scope)
13print(scope["total"])  # 10

This is useful when the same source is executed many times. You compile once, then reuse the code object instead of reparsing the string repeatedly.

The mode argument matters:

  • '"eval" expects a single expression'
  • '"exec" expects statements or a code block'
  • '"single" is mainly for interactive-style input'

Choose the right tool

A good rule is:

  • if you need a value from one expression, use eval
  • if you need side effects from statements, use exec
  • if you need reusable compiled code, use compile

Often, though, the best answer is "use none of them." If you can call a function, parse JSON, or use a normal dispatch table, that is usually clearer and safer than running dynamically generated Python code.

Common Pitfalls

  • Running untrusted input through eval or exec and assuming restricted namespaces make it safe.
  • Using eval for statements such as assignments or loops and then being surprised by a syntax error.
  • Expecting exec to return the result of the last expression when it only performs side effects.
  • Calling compile without a real reuse or tooling need and adding unnecessary complexity.
  • Reaching for dynamic execution when a normal function call, parser, or dispatch table would be clearer.

Summary

  • 'eval runs a single expression and returns its value.'
  • 'exec runs statements and code blocks for their side effects.'
  • 'compile turns source text into a code object for later execution.'
  • The mode passed to compile must match the kind of source you are compiling.
  • 'eval and exec are dangerous with untrusted input and should be avoided unless dynamic execution is truly necessary.'

Course illustration
Course illustration

All Rights Reserved.