In this article, we will see how to call a function of a module by using its name (a string) in Python. Basically, we use a function of any module as a string, let’s say, we want to use randint() function of a random module, which takes 2 parameters [Start, End] and generates a random value between start(inclusive) and end(inclusive). Here, we will discuss 2 methods to solve this:
- Python Getattr() Method
- Python exec() Method
What is Getattr() Method
Python getattr() function is used to access the attribute value of an object and also gives an option of executing the default value in case of unavailability of the key.
Syntax of getattr() method
Syntax: getattr(obj, key, def)
- obj: The object whose attributes need to be processed.
- key: The attribute of object
- def: The default value that needs to be printed in case the attribute is not found.
Returns: Object value if the value is available, the default value in case attribute is not present, and returns AttributeError in case attribute is not present and the default value is not specified.
Example 1
Importing a module random and using getattr() method to access randint method of random.
Python3
if __name__ = = "__main__" : # Importing a module random import random # Using randint function of random # module as r r = getattr (random, 'randint' ) # calling the function and storing # the value in res res = r( 1 , 9 ) # Printing the result print (res) |
Output:
9
Example 2
Here, we are using randint function of the random module as func.
Python3
if __name__ = = "__main__" : # Importing a module random Module = __import__ ( 'random' ) func = getattr (Module, 'randint' ) # calling the function and storing # the value in res res = func( 1 , 9 ) # Printing the result print (res) |
Output:
6
What is exec() method
The exec() function is used for the dynamic execution of Python programs which can either be a string or object code. If it is a string, the string is parsed as a suite of Python statements which is then executed unless a syntax error occurs and if it is an object code, it is simply executed. We must be careful that the return statements may not be used outside of function definitions not even within the context of code passed to the exec() function. It doesn’t return any value, hence returns None.
Syntax of exec() method
Syntax: exec(object[, globals[, locals]])
- object: As already said this can be a string or object code
- globals: This can be a dictionary and the parameter is optional
- locals: This can be a mapping object and is also optional
Return: None.
Example 1
Using the random function of the random module and calling it using exec() method. The random module generates a decimal number between 0 and 1.
Python3
if __name__ = = "__main__" : # Importing a module random import random # Using random function of random # module as func func = "random.random" # calling the function and storing # the value in res exec (f "x = {func}" ) res = x() # Printing the result print (res) |
Output:
0.640045711797753
Example 2
Here also we are generating a decimal number between 0 and 1.
Python3
if __name__ = = "__main__" : # Importing a module random import random # Using random function of random # module as func func = "random" # calling the function and storing # the value in res exec (f "x = random.{func}" ) res = x() res1 = round (res, 4 ) # Printing the result print (res1) |
Output:
0.6221