Last Updated : 23 Jul, 2025
Python provides multiple ways to evaluate expressions and convert data from one format to another. Two commonly used methods are eval() and ast.literal_eval(). While they might appear similar, they serve very different purposes and have significant security implications. This article explores the key differences between eval() and ast.literal_eval(), their use cases, security concerns and best practices.
What is eval()?eval() is a built-in Python function that parses and evaluates expressions passed as a string. It can handle a wide variety of inputs, including arithmetic expressions, function calls, and even arbitrary code execution.
Syntax of eval()eval(expression, globals=None, locals=None)
Example:
Python
exp = "2 + 3" # expression
res = eval(exp)
print(res)
Explanation: In this example, eval() evaluates the string "2 + 3" and returns the result 5.
Key Features of eval()ast.literal_eval() is a function from Python's ast (Abstract Syntax Tree) module. It safely evaluates a string containing a Python literal or a container object. Unlike eval(), it only processes basic literals like strings, numbers, lists, tuples, dictionaries, booleans, and None. It raises an error if the input contains anything beyond these, making it significantly safer.
Syntax of ast.literal_eval()import ast
ast.literal_eval(node_or_string)
The Abstract Syntax Tree (AST) is a representation of source code structure. It breaks down the syntax into a tree-like format where each node represents a component of the syntax. The ast module provides access to Python’s AST and literal_eval() ensures that only safe literals are evaluated. Example:
Python
import ast
# Converting a string representation of a list to an actual list
s = "[1, 2, 3, 4]"
res = ast.literal_eval(s)
print(res)
Explanation: In this example, ast.literal_eval() converts the string representation of a list into an actual list.
How does ast.literal_eval() work?ast.literal_eval() parses the string into an AST syntax tree and verifies that it contains only valid literals. If an unsafe operation (such as function calls or execution commands) is found, it raises an exception. Example:
Python
import ast
# Safe string evaluation
safe_expression = '{"name": "Aditya", "age": 24}'
res = ast.literal_eval(safe_expression)
print(res)
# Unsafe expression (raises ValueError)
unsafe_expression = "os.system('rm -rf /')"
try:
res = ast.literal_eval(unsafe_expression)
except ValueError as e:
print("Error:", e)
{'name': 'Aditya', 'age': 24} Error: malformed node or string on line 1: <ast.Call object at 0x7f27f021e2d0>
Explanation: ast.literal_eval() safely evaluates only literals (strings, numbers, tuples, lists, dicts, booleans, None). It converts a JSON-like string into a dictionary and prevents arbitrary code execution by raising a ValueError for unsafe expressions.
Key Features of ast.literal_eval()RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4