What is Python exec() Function?

Python exec() is a built-in function that allows you to dynamically execute Python code at runtime. It takes a string containing Python code as input and executes it as if it were a regular Python script. This dynamic code execution enables you to perform tasks such as evaluating expressions, defining functions, creating classes, and more, all during the program’s execution.

What is the Purpose of exec() ?

The primary purpose of Python exec() is to provide a way to execute Python code that is not known until runtime. It gives developers the flexibility to write programs that can adapt and respond to user input or other external factors dynamically.

Before you explore the real-life instances of Python exec() function, let’s first analyze its syntax and parameters, which are essential in carrying out the examples.

Python exec() Syntax and Parameters

The syntax of the Python exec() is simple and easy to use. An example is provided below to illustrate the syntax of the exec() function.

exec(object, globals, locals)

When utilizing Python exec() function, keep in mind that it requires three parameters, with the object being mandatory, while globals and locals are optional. Let’s delve into each parameter to understand its functionalities more closely:

I. Object

Object parameter represents the Python code that you want to execute. It should be provided as a string containing Python statements, expressions, or even complete scripts. This code will be dynamically evaluated and executed during runtime.

II. Globals (Optional)

Globals parameter is a dictionary that serves as the global namespace during code execution. If you don’t provide this parameter, the function will automatically use the globals() function to retrieve the current global namespace. This means that the code executed with exec() can access and modify global variables in the same way as regular Python code.

III. Locals (Optional)

Locals parameter is a dictionary that acts as the local namespace during code execution. If you don’t provide this parameter, the function will automatically use the locals() function to fetch the current local namespace. It means that the code executed with exec() can access and modify local variables in the same way as regular Python code within the specified context.

Now that you have acquired a solid understanding of the function’s purpose, syntax, and parameters, it’s time to explore its return value and witness Python exec() in action!

Python exec() Return Value

When using Python exec() function, you won’t receive any explicit return value. Instead, it will return None after executing the specified Python code dynamically. Unlike other built-in functions that provide a result after performing certain operations, exec() is primarily used for its side effects, allowing you to execute Python code on-the-fly.

Now that you have a good grasp of the Python exec() function’s syntax, parameters, and return value, it’s time to delve into practical examples to explore its functionalities. Through these examples, you will gain a clearer understanding of how the exec() function works and how you can leverage its flexibility in your Python programs.

What Does exec() Function Do?

Python exec() allows you to dynamically execute Python code, which means you can run Python code stored as strings at runtime. It takes a string containing valid Python code as input and executes it as if it were part of the regular Python script. Let’s examine some examples to better grasp the functionality of the Python exec() function.

I. Creating an exec() Object

Python exec() function doesn’t create an object in the traditional sense, as its primary purpose is to execute Python code rather than returning a value. When you call exec(), it directly interprets and runs the provided code without creating a separate object to hold the result. Instead, it directly modifies the global and/or local namespaces and performs the actions specified in the given code.

However, if you try to create an object of exec() like a regular function or class, it will raise a TypeError because it is not meant to be instantiated. Here’s an example that showcases the error:

Example Code
try: my_exec = exec() except TypeError as e: print("Error:", e)

In this example, we are attempting to create an object named my_exec of the exec() function. However, this will raise a TypeError because exec() is not meant to be instantiated like a regular function or class. The try block is used to handle any potential exceptions that might occur during the execution of the code inside it. In this case, we expect a TypeError to be raised when trying to create my_exec with no arguments.

If a TypeError is indeed raised, the except block will be executed. It catches the TypeError exception and stores it in the variable e. Then, it prints an error message indicating that the function exec() requires at least one argument to be passed.

Error: exec expected at least 1 argument, got 0

This error message clarifies that you cannot create an object of exec() without providing any arguments, as it is not intended to be used that way. Instead, you can use exec() to dynamically execute Python code from strings, passing the code as an argument to the function.

II. Utilizing exec() for Statements and Expressions

Python exec() is not limited to executing complete programs; it can also handle individual statements and expressions. This flexibility allows you to evaluate expressions and perform specific tasks without the need for a full-fledged program. Let’s explore how to use exec() for different types of statements and expressions.

Example Code
statement_code = "result = 10 + 5" exec(statement_code) print("The result of the statement is:", result) expression_code = "10 * 5" result = exec(expression_code) print("The result of the expression is:", result)

Here, first we define the statement_code variable and set it to the string result = 10 + 5. Using the exec() function, we execute this statement dynamically. As a result, the variable result is created and assigned the value of 15. Since we used exec() to execute the statement directly, we can now print the result of the statement by accessing the value stored in result.

Next, we have the expression_code variable set to the string 10 * 5. We attempt to use exec() to evaluate this expression, but exec() doesn’t return any value. Therefore, when we try to assign the result of exec(expression_code) to the result variable then it generated None.

The result of the statement is: 15
The result of the expression is: None

By using exec() in this manner, you can dynamically perform mathematical calculations, making your code more flexible and interactive.

III. Exec() with Single Statement

In Python, when using exec() with a single statement, it allows you to dynamically execute a single line of Python code provided as a string. Python exec() function takes the string containing the Python statement, interprets it as code, and executes it during runtime. This enables you to perform various tasks, such as modifying variables, printing values. The single statement passed to exec() can be any valid Python statement, like assigning a value to a variable, performing arithmetic operations, or calling a function. For Example:

Example Code
single_statement_code = "print('Hello, Python Helper!')" exec(single_statement_code)

For this example, we have a single Python statement stored in the variable single_statement_code. The statement is responsible for printing the message to the screen. By using the exec() function with single_statement_code as an argument, we dynamically execute this statement. As a result, the message is displayed in the screen.

Hello, Python Helper!

By using this approach, you can dynamically execute Python statements and achieve more flexibility in your code. exec() allows you to run arbitrary code at runtime, which can be useful in various scenarios, such as building dynamic scripts, interactive programs, or even code generation tasks.

IV. Exec() with Multi Statement

Python exec() with multiple statements allows you to dynamically execute a sequence of Python statements provided as a single string. This can include variable assignments, function definitions, loops, and other complex logic. The exec() function parses the input string and executes each statement sequentially, just as if the statements were written directly in the Python script. This flexibility enables you to dynamically generate and execute code at runtime, making your program more adaptable. Let’s explore an example below:

Example Code
# Initialize the flag for the while loop should_continue = True while should_continue: # Read a multi-statement block of code from user input code_input = input("Enter Python expression (use 'q' to quit):\n") if code_input.lower().strip() == 'q': print("Exiting the loop.") should_continue = False else: try: exec(code_input) except Exception as e: print("Error:", e)

In this example, we have a Python program that utilizes the exec() function with a while loop to dynamically execute multiple lines of Python code entered by the user. The program starts by prompting the user to enter a block of Python code. It then initializes a flag called should_continue to True, indicating that the loop should keep running.

Inside the while loop, we check if the user input is ‘q‘ (case-insensitive and stripped of leading/trailing spaces) to exit the loop. If ‘q‘ is entered, the program prints a message indicating that the loop is exiting, and we set should_continue to False, causing the loop to terminate.

However, if the user doesn’t enter ‘q‘, we attempt to execute the input code using exec(code_input). If there is an error during the execution of the input code, the program catches the exception with a generic Exception handler and prints the error message.

After executing the code or handling any errors, the program again prompts the user to enter another block of Python code. This process continues until the user decides to exit the loop by typing ‘q‘.

Enter Python expression (use ‘q’ to quit):
Enter Python expression (use ‘q’ to quit):
Enter Python expression (use ‘q’ to quit):
Enter Python expression (use ‘q’ to quit):
Exiting the loop.

With this approach, the user can interactively input and execute multiple lines of Python code in a dynamic and iterative manner.

V. Python Exec() with Dynamic Function

Python exec() with dynamic function allows you to create and define functions dynamically at runtime. This means you can construct the function’s code as a string and then use exec() to execute that code, efficiently defining the function programmatically. This dynamic approach is flexible, enabling you to create custom functions tailored to specific requirements or scenarios. Now, let’s examine an example to illustrate its usage:

Example Code
# Create a function dynamically and call it function_name = "greet" function_code = f'def {function_name}():\n print("Hello, Dynamic Function!")' exec(function_code) # Call the dynamically created function greet()

For this example, we are exploring the concept of dynamic function creation using exec() in Python. First, we define a function name, which we set as greet. Then, we construct the code for the function as a string using an f-string. The function_code variable holds the string with the definition of the greet function, which, in this case, simply prints Hello, Dynamic Function! when called.

Next, we use exec() to execute the function_code, which creates the greet function dynamically during runtime. After executing the exec() statement, the greet function becomes available in the current namespace, and we can call it like any other function in our program. Finally, we call the dynamically created greet function by using its name greet() as we would with any regular function. When the function is called, it prints result on the screen.

Hello, Dynamic Function!

As you can see in the above example, using exec() to create and call functions dynamically allows for greater flexibility and adaptability in your Python programs.

Now that you possess a solid grasp of the Python exec() function and its illustrated functionalities in the preceding examples, it’s crucial to note that it does not support non-primitive data types. Executing the exec() function with non-primitive data types will result in a return value of None. With this foundation in place, let’s explore more advanced examples that showcase the flexibility of the exec() function.

Python exec() Advanced Examples

Let’s delve into some advanced examples of Python exec(), showcasing its flexible applications. These examples will highlight the flexibility of exec() and showcase its convenience in addressing various programming scenarios in Python.

I. Handling Complex Python Code with exec()

By employing exec() in Python, you are not limited to simple statements; you can handle more complex code with multiple lines and indents. This flexibility makes exec() invaluable for dynamically generating and executing larger code blocks based on runtime conditions or user input. It allows you to create dynamic and adaptive applications that can adjust their behavior and perform various tasks on the fly, enhancing the flexibility of your Python programs.

Here’s an example of how you can use Python exec() function to handle complex Python code that calculates the factorial of a given number:

def factorial(num):
     result = 1
     while num > 1:
           result *= num
           num -= 1
     return result

def calculate_factorial_with_exec():
     num = int(input("Enter a number: "))
     code_to_execute = f'''
def factorial(num):
     result = 1
     while num > 1:
           result *= num
           num -= 1
     return result

result = factorial({num})
print("The factorial of {num} is:", result)



Here, we define a function calculate_factorial_with_exec(). Inside this function, we take user input for a number, and then we dynamically generate a Python code block as a string using f-strings. The generated code block calculates the factorial of the input number using the factorial() function we defined earlier. Finally, we use exec() to execute the dynamically generated code, which prints the factorial of the entered number to the screen.

Enter a number: 5
The factorial of 5 is: 120

By using this approach with exec(), you can dynamically generate and execute complex Python code, allowing you to handle various calculations and tasks without the need for pre-defined functions.

II. Passing Variables and Namespaces to exec()

One of the significant advantages of exec() is the ability to pass variables and control the namespace in which the code is executed. This allows you to create isolated environments for executing the code. Here’s an example using exec() to determine if a number is prime or not by passing variables and namespaces:

num = int(input("Enter a number: "))

namespace = {'num': num}

prime_check_code = '''
if num <= 1:
     print(num, "is not a prime number.")
     is_prime = True
     for i in range(2, num):
           if num % i == 0:
                 is_prime = False
     if is_prime:

           print(num, "is a prime number.")
           print(num, "is not a prime number.")

exec(prime_check_code, namespace)

For this example, we use exec() to dynamically generate code for checking whether the given number is prime or not using if conditions. The namespace dictionary is passed as the locals parameter to exec() to provide access to the variable num within the executed code. The program checks if the number is less than or equal to 1 (not prime), and then it iterates from 2 to num-1 to check for factors. If no divisors are identified, the number is considered prime.

Enter a number: 23
23 is a prime number.

By using this approach, you can dynamically determine if a given number is prime or not, providing you with a flexible and interactive way to handle prime number checks in your Python programs.

III. Handling Exceptions and Errors with exec()

When you execute dynamically provided code, you run the risk of encountering errors and exceptions. To avoid program crashes and ensure smooth execution, it’s crucial to handle these exceptions in a graceful manner. By using try-except blocks, you can catch and handle potential errors, allowing your program to recover gracefully and continue functioning. For example:

Example Code
class DynamicCodeExecution: def __init__(self, code): self.code = code def execute_code(self): try: exec(self.code) except ZeroDivisionError as e: print("Error occurred:", e) # Usage of the class code = 'print(10 / 0)' executor = DynamicCodeExecution(code) executor.execute_code()

In this example, we define a class called DynamicCodeExecution, and its constructor takes a Python code as input and stores it in an instance variable called code. We also implement a method named execute_code(), which attempts to execute the stored code using the exec() function. Inside the method, we have a try block to handle potential exceptions during code execution, specifically catching the ZeroDivisionError.

For the example usage, we create an instance of DynamicCodeExecution with the code ‘print(10 / 0)‘, which involves a division by zero and triggers a ZeroDivisionError. Then, we call the execute_code() method on this instance, resulting in the execution of the provided code. When the ZeroDivisionError is encountered, the except block is executed, and we print an error message indicating that an error occurred during code execution.

Error occurred: division by zero

As you can see, the DynamicCodeExecution class allows you to handle exceptions gracefully when executing dynamically provided code. By encapsulating the code execution within the class, you can easily manage errors and ensure that your program doesn’t crash due to exceptions like the division by zero error in this example.

IV. Restricting Available Methods and Variables in exec()

When you are using exec() function then in some cases, you may want to provide a limited set of methods and variables that are safe to use within the dynamically executed code. This can be achieved by passing appropriate globals and locals dictionaries to exec(). Consider the following example to better understand its functionality:

restricted_namespace = {'add': lambda a, b: a + b}

# Code to execute
code_to_execute = '''
result = add(5, 3)
print("Sum of 5 and 3 is:", result)

# Execute the code with the restricted namespace
exec(code_to_execute, restricted_namespace)

Here, we are showcasing how to use exec() while restricting access to specific functions through the use of a restricted namespace. First, we create a restricted_namespace dictionary containing only the add function. The function is a lambda function that accepts two arguments, ‘a‘ and ‘b,’ and yields the sum of the two values.

Next, we have the code_to_execute variable, which contains a multi-line string representing the Python code we want to execute dynamically. Inside this code, we call the add function from the restricted namespace with the arguments 5 and 3, and store the result in the result variable.

Finally, we use exec() to execute the code_to_execute with the restricted namespace restricted_namespace. This means that only the add function is accessible during code execution, and other functions or variables from the global namespace are not accessible. As a result, the code executes successfully.

Sum of 5 and 3 is: 8

This approach provides a safer way to dynamically execute code, as it prevents unwanted access to other functions and variables in the global namespace, thus mitigating potential security risks.

There are two important aspects of the restriction of available methods and variables in exec() function that are crucial for you to understand. Let’s explore them now to enhance your understanding and usage of the exec() function.

A. When both Globals and Locals Parameters are Ignored

If you choose to ignore both the globals and locals parameters when using exec(), the code will be executed in the current global and local namespaces that are active at the time of execution. For example:

Example Code
# When both globals and locals are ignored x = 5 def demo(): y = 10 code = 'print("Value of x:", x)' # Accessing x from the global namespace exec(code) demo()

For this example, we have a global variable x with the value 5. Inside the demo() function, we have a local variable y with the value 10. When we use exec() to execute the code represented by the string code, it accesses the global variable x and prints its value, which is 5. Even though the x variable is not explicitly passed through the globals or locals parameters of exec(), it is automatically available because exec() uses the current global and local namespaces when these parameters are not provided. As a result, the code prints Value of x: 5 when the demo() function is called.

Value of x: 5

As you can see, even without explicitly passing x as a global or local variable to exec(), the code successfully accessed and printed the value of x, illustrating that exec() uses the current global and local namespaces when the globals and locals parameters are ignored.

B. Passing Globals Parameter & Locals Parameter Omitted

When you omit both the globals and locals parameters in the exec() function, it automatically uses the current global and local namespaces to resolve variables and execute the code. This grants the code access to all the variables in the current scope. For example:

Example Code
x = 10 def execute_code(): y = 5 code = "print('Sum of x and y:', x + y)" # Accessing x and y from the global and local namespaces exec(code) execute_code()

In this scenario, we have a global variable x and a local variable y inside the execute_code() function. Since we haven’t explicitly provided the globals and locals parameters to exec(), it automatically uses the current global and local namespaces. As a result, the code can access both x and y, and when executed, it prints the sum of x and y.

Sum of x and y: 15

As you can see in the above output, the exec() function successfully accessed both the global variable x and the local variable y, showcasing how it utilizes the current global and local namespaces by default.

Having gained a thorough understanding of Python’s exec() function, its applications, and its adaptability in diverse situations, you now possess a solid groundwork. To deepen your knowledge, let’s explore some theoretical concepts that will be immensely valuable in your journey of Python programming.

Security Considerations with exec()

While Python exec() function offers great flexibility and dynamic code execution, it also presents potential security risks if used carelessly. As the code executed by exec() can come from external sources or user inputs, you should be cautious to prevent code injection and unauthorized access.

I. Avoiding Code Injection Vulnerabilities

To protect against code injection, it is crucial to follow best practices and avoid executing untrusted code directly. Whenever you encounter a situation where you need to use Python exec() with user-provided inputs, ensure thorough validation and sanitization of those inputs beforehand.

II. Restricting Available Methods and Variables

When you using exec(), it’s a good practice to restrict the available methods and variables within the executed code. This can be achieved by passing limited globals and locals dictionaries, containing only the necessary functions and variables, or by using the locals parameter to create a separate namespace for the executed code.

III. Limiting Access to Sensitive Information

To safeguard your application’s security, refrain from executing code that may access sensitive information or perform critical operations. It is essential to limit the scope of execution to only necessary functionalities and avoid passing crucial data like database connection strings or API keys directly to Python exec().

IV. Regularly Reviewing and Auditing Dynamic Code

If your application relies heavily on dynamically executed code, it’s crucial to periodically review and audit the code to identify potential security risks and vulnerabilities. Regularly update and validate the code to ensure it remains secure against evolving threats.

Limitations and Restrictions of exec()

Although Python exec() function is an amazing feature, it comes with some limitations and restrictions that you should be aware of:

I. Performance Considerations

Using exec() can have an impact on the performance of your application, especially when executing complex code or running it repeatedly. Extensive use of exec() can lead to reduced performance and increased memory usage.

II. Compatibility with Compiled Code

When utilizing Python exec() to execute code, keep in mind that it might not be as optimized or compiled as regular Python code. Due to this, some operations that would otherwise be optimized in standard Python code may be slower when executed through exec(). While exec() offers dynamic flexibility, it’s essential to be mindful of potential performance trade-offs and use it judiciously for optimal efficiency in your applications.

III. Debugging and Error Handling

Debugging dynamically executed code can be challenging for you. When an error occurs during execution, it might be difficult to pinpoint the exact location in your code where the error originated. Proper error handling and debugging techniques should be employed to address this issue.

Congratulations! You’ve now learned Python exec() function, how it allows you to dynamically execute code during runtime. With exec(), you can perform tasks like evaluating expressions, defining functions, and creating classes on-the-fly, making your programs more flexible and adaptive.

To use exec() convinently, it’s essential to understand its syntax and parameters. Remember that it requires three parameters: the object containing the code to execute (provided as a string), the globals parameter (optional) representing the global namespace, and the locals parameter (optional) for the local namespace.

Be cautious while using exec() and avoid executing untrusted code directly, as it can pose security risks. Always validate and sanitize inputs to prevent code injection vulnerabilities. Remember, with great power comes great responsibility. By mastering Python exec() function, you’re equipping yourself with a valuable tool to create dynamic, adaptive, and powerful Python programs. So, go ahead and unleash your creativity with exec(), and let your code dynamically transform and respond to challenges! Happy coding!

Scroll to Top