eval(expression, globals=None, locals=None) is a built-in function that takes a string containing a single Python expression, compiles it, runs it and returns the result. For example eval("2 + 3 * 4") returns 14.
It is also one of the most dangerous built-ins. Anything you pass to eval runs with the full power of your program, so you must never use it on input that comes from a user, a file you do not control or the network. For parsing data, use ast.literal_eval, json.loads or a dedicated parser instead.
The parameters are:
expression: a string (or a code object) holding one expression. Statements such as x = 1, import os or for loops raise SyntaxError.globals: a dictionary used as the global namespace. If omitted, the caller's globals are used.locals: any mapping used as the local namespace. If omitted, it defaults to globals when globals is given, otherwise to the caller's locals.| Function | Accepts | Returns |
|---|---|---|
eval() | One expression | The value of the expression |
exec() | Any code, including statements | Always None |
Use exec to run statements and eval to get a value. Both carry the same security risk.
eval runs arbitrary code. If the string contains user input, an attacker controls your process:
Passing an empty globals dictionary or setting {"__builtins__": {}} does not make eval safe. Python objects expose enough attributes (for example through ().__class__.__mro__) for a determined attacker to climb back to powerful objects. Treat sandboxing eval as unsolved, and avoid it for untrusted data.
ast.literal_eval evaluates a string that contains only Python literals: strings, numbers, tuples, lists, dicts, sets, booleans and None. It does not call functions or look up names.
It is meant for trusted-ish config values and literal reprs, not as a general security boundary: very deeply nested or huge input can still exhaust memory or the stack.
For data from outside your program, use json.loads. It is a real parser with a defined format.
If you need arithmetic from user input, parse it with ast and allow only the operations you expect.
This accepts only numbers and the listed operators. Anything else, such as a function call or a name, raises ValueError. Large exponents such as 9 ** 9 ** 9 can still consume a lot of CPU time, so add limits in a real application.
Use eval only when all of these are true:
getattr, dictionaries of functions and ast.literal_eval cover most dynamic needs.Interactive tools such as debuggers and notebooks use eval and exec legitimately because the user already has full control of the process.
It evaluates one Python expression given as a string and returns its value. Statements are not allowed.
No. Clearing __builtins__ or passing empty namespaces slows an attacker down but does not stop them. Use ast.literal_eval, json.loads or a custom parser for untrusted input.
eval runs any expression, including function calls. ast.literal_eval accepts only literal structures and raises ValueError for anything else, which makes it far safer for parsing data.
Not directly. eval accepts a single expression. Use exec for statements, with the same security concerns.
eval turns a string into a value by running it as Python code, which makes it flexible and risky. Keep it away from untrusted input, prefer ast.literal_eval for literals and json.loads for data, and write a small ast based evaluator when users need arithmetic.