Call Function of a Module from String in Python



In Python, you can dynamically call a function from a module by using its name as a string. This is useful in scenarios where function names are not known until runtime. Below are several methods to achieve this.

Using 'getattr()' Function

The getattr() function is a built-in Python function that allows you to retrieve an attribute from an object using a string representing the attribute's name. This is particularly useful when you want to dynamically call functions or access attributes from a module or class based on their names as strings.

Example

The following example demonstrates how the getattr() function retrieves the function 'bar' as an object.

# Assuming we have a module named foo.py with the following content:
# def bar():
#     return "Hello from bar!"
import foo
# The function name as a string
function_name = 'bar'

# Get the function using getattr
method_to_call = getattr(foo, function_name)

result = method_to_call()
print(result)

Following is the Output for the above code.

Hello from bar!  

Using 'globals()' Function

The globals() function in Python is used to return a dictionary representing the current global symbol table. This table includes all global variables and their values that are defined in the current module. To access global variables and their values from anywhere in your code.

Example

In the following function, we will retrieve the function from the global namespace using 'globals()' and call it.

def my_function():
    return "Hello from my_function!"

# The function name as a string
function_name = 'my_function'

# Call the function using globals()
result = globals()[function_name]()
print(result)

Following is the output for the above code.

Hello from my_function!

Using 'locals()' Function

We can use locals() in Python to access local variables within the current scope. It returns a dictionary that represents the current local symbol table, essentially allowing you to see all local variables, their names, and functions.

Example

In the following code, inside 'call_function', we use locals() to access 'another_function' and call it.

def another_function():
    return "Hello from another_function!"

def call_function():
    # The function name as a string
    function_name = 'another_function'
    
    # Call the function using locals()
    result = locals()[function_name]()
    print(result) 

call_function()

Following is the output for the above code.

Hello from another_function!

Using importlib Module

The importlib module in Python provides a way to import modules programmatically. This can be particularly useful when you need to load a module at runtime based on conditions or configurations.

Example

In the following example, we used 'importlib.import_module()' to import the module 'foo' dynamically. Then, we use `getattr()` to call the function `bar`.

import importlib

# The module name and function name
module_name = 'foo'
function_name = 'bar'

# Import the module dynamically
foo_module = importlib.import_module(module_name)

# Call the function using getattr
result = getattr(foo_module, function_name)()
print(result)

Following is the output for the above code.

Hello from bar!
Updated on: 2025-02-25T11:23:09+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements