Skip to content
UPEEC

eval(), exec() and compile()

eval evaluates an expression string, exec runs statements, compile turns source into a code object.

Intermediate
Comments 0
eval("2 + 3 * 4")          # 14
exec("x = 5")
code = compile("a * b", "<expr>", "eval")

Never pass untrusted input to eval/exec — it can run any code. Use ast.literal_eval to safely parse literals like "[1, 2]".

Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.

Discussion (0)

Sign in or join free to ask questions and discuss this topic.