![]() |
VOOZH | about |
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.
Table of Content
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.
eval(expression, globals=None, locals=None)Example:
5
Explanation: In this example, eval() evaluates the string "2 + 3" and returns the result 5.
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.
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:
[1, 2, 3, 4]
Explanation: In this example, ast.literal_eval() converts the string representation of a list into an actual list.
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:
{'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.
| Feature | eval() | ast.literal_eval |
|---|---|---|
| Purpose | Evaluate arbitrary Python expressions and code. | Safely evaluate Python literals like strings, numbers, lists, etc. |
| Security | Unsafe, as it can execute arbitrary code. | Safe, as it only evaluates literals and raises an error for non-literal expressions. |
| Scope | Can evaluate any valid Python code, including function calls and loops. | Only evaluates simple data structures and literals. |
| Use Cases | Dynamic code execution, real-time expression evaluation. | Parsing input that represents basic Python data types safely. |
| Performance | Slower due to dynamic nature and broad functionality. | Faster since it only evaluates literals. |
| Error Handling | May silently execute malicious code if not handled properly. | Raises an error if the input isn't a valid Python literal. |