Skip to content

[Edit]: Python eval() #7205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 120 additions & 29 deletions content/python/concepts/built-in-functions/terms/eval/eval.md
Original file line number Diff line number Diff line change
@@ -1,64 +1,155 @@
---
Title: 'eval()'
Description: 'Returns the value of a Python expression passed as a string.'
Description: 'Evaluates and executes Python expressions from string input dynamically.'
Subjects:
- 'Computer Science'
- 'Data Science'
Tags:
- 'Functions'
- 'Methods'
- 'Python'
- 'Strings'
CatalogContent:
- 'learn-python-3'
- 'paths/data-science'
- 'paths/computer-science'
---

The **`eval()`** function returns the value of a Python expression passed as a [string](https://www.codecademy.com/resources/docs/python/strings).
The **`eval()`** function is a built-in Python function that dynamically evaluates and executes Python expressions from [string](https://www.codecademy.com/resources/docs/python/strings)-based input. This powerful function parses a string containing a Python expression, compiles it into bytecode, and then evaluates it as if it were written directly in the code.

## Security Concerns
The `eval()` function is commonly used in scenarios where there is a need to evaluate mathematical expressions from user input, create dynamic code execution systems, or process expressions stored as strings. However, it should be used with caution due to significant security implications when handling untrusted input.

While `eval()` can be useful, care must be taken to understand the security implications of this function. If `eval()` is used with user-generated strings, a malicious user could execute arbitrary code through the function. Good programming practice generally advises against using `eval()`. If it is used, it should never be used with untrusted input.
> **Note:** Never use `eval()` on untrusted input without strict validation or sandboxing, as it can execute arbitrary code and pose major security risks.

## Syntax

```pseudo
eval(expression, globals, locals)
```

The `eval()` function uses the following parameters:
**Parameters:**

- `expression`: The expression to evaluate.
- `globals` (optional): A [dictionary](https://www.codecademy.com/resources/docs/python/dictionaries) defining which [variables](https://www.codecademy.com/resources/docs/python/variables) are in the `expression`'s global [scope](https://www.codecademy.com/resources/docs/python/scope). If `globals` isn't specified, `eval()` uses the current global scope.
- `locals` (optional): A dictionary defining the variables in the `expression`'s local scope. If the `locals` argument is specified, the `globals` argument must be specified as well.
- `expression`: A string containing a valid Python expression to be evaluated
- `globals` (optional): A [dictionary](https://www.codecademy.com/resources/docs/python/dictionaries) specifying the global namespace for the evaluation
- `locals` (optional): A dictionary specifying the local namespace for the evaluation

> **Note:** While using the `globals` argument overrides the user defined variables available, if it doesn't specify a value for the `__builtins__` key, then a reference for it is automatically added so that `eval()` will have access to all of Python's built-in names when evaluating `expression`.
>
> **Note:** `eval()` does not support keyword arguments and it doesn't work on compound statements or assignment operations. It only works with expressions that can be evaluated to be equal to some value.
**Return value:**

## Example
The `eval()` function returns the result of the evaluated expression. The return type depends on the expression being evaluated.

The following example uses `eval()` to evaluate an expression using variables in the current global scope, then evaluates the same expression with its own global scope:
## Example 1: Basic Mathematical Evaluation

This example demonstrates the fundamental usage of `eval()` for evaluating simple mathematical expressions:

```py
x = 10
y = 5
# Evaluating basic arithmetic expressions
result1 = eval("5 + 3")
print(f"5 + 3 = {result1}")

result2 = eval("10 * 2 - 5")
print(f"10 * 2 - 5 = {result2}")

print(eval("x + y")) # Output: 15
print(eval("x + y", {"x":15, "y":y})) # Output: 20
# Evaluating expressions with variables
x = 4
result3 = eval("x ** 2 + 1")
print(f"x ** 2 + 1 = {result3}")
```

## Codebyte Example
The output of this code is:

In the examples below, the `eval()` function is used to return a value from a string:
```shell
5 + 3 = 8
10 * 2 - 5 = 15
x ** 2 + 1 = 17
```

```codebyte/python
x = 2
y = 3
This example shows how `eval()` can process mathematical expressions stored as strings and return their computed values.

## Example 2: Dynamic Calculator Application

This example demonstrates a real-world scenario where `eval()` can be used to create a simple calculator that processes user input:

```py
def safe_calculator():
# Define allowed mathematical operations
allowed_names = {
'abs': abs, 'round': round, 'min': min, 'max': max,
'sum': sum, 'pow': pow, 'sqrt': lambda x: x ** 0.5
}

print("Simple Calculator (type 'quit' to exit)")

while True:
try:
# Get user input
expression = input("Enter expression: ")

if expression.lower() == 'quit':
break

# Evaluate with restricted namespace for safety
result = eval(expression, {"__builtins__": {}}, allowed_names)
print(f"Result: {result}")

except Exception as e:
print(f"Error: {e}")

print(eval("x * y"))
print(eval("x * y", {"x": 3, "y": y}))
print(eval("min([-10, 20, 45])"))
# Example usage
# User inputs: "25 + 17", "sqrt(144)", "pow(2, 3)"
```

This example shows how `eval()` can create an interactive calculator while using restricted namespaces to improve security.

## Codebyte Example: Configuration File Processing

This example illustrates how `eval()` can be used to process configuration values stored as strings:

# The following example will create a TypeError as the first argument must be a string
# print(eval(x*y))
```codebyte/python
import json

def process_config(config_data):
"""Process configuration with dynamic evaluation"""

# Sample configuration with expressions
config = {
"max_connections": "50 * 2",
"timeout_seconds": "30 + 15",
"cache_size": "1024 * 1024",
"debug_mode": "True"
}

processed_config = {}

# Safe namespace for evaluation
safe_globals = {"__builtins__": {}}
safe_locals = {"True": True, "False": False}

for key, value in config.items():
try:
# Evaluate the expression safely
processed_config[key] = eval(value, safe_globals, safe_locals)
print(f"{key}: {value} -> {processed_config[key]}")
except:
# If evaluation fails, keep original string
processed_config[key] = value
print(f"{key}: {value} (kept as string)")

return processed_config

# Execute the function
result = process_config({})
```

This example demonstrates how `eval()` can dynamically process configuration values while maintaining security through restricted namespaces.

## Frequently Asked Questions

### 1. What's the difference between `eval()` and `exec()`?

`eval()` evaluates expressions and returns a value, while `exec()` executes statements and returns None. Use `eval()` for expressions like "2 + 3" and `exec()` for statements like "x = 5".

### 2. Can `eval()` handle multi-line expressions?

No, `eval()` only works with single expressions. For multi-line code execution, use `exec()` instead.

### 3. What happens if `eval()` receives invalid syntax?

`eval()` raises a `SyntaxError` exception if the expression contains invalid Python syntax.