EN
Python - call function of a module by using its name as a string
0 points
In this article, we would like to show you how to call a function of a module by using its name as a string in Python.
Quick solution:
xxxxxxxxxx
1
import math
2
3
method = getattr(math, "sqrt")
4
print(method(16)) # 4.0
In this example, we use getattr()
method to get the sqrt
method from math
module.
xxxxxxxxxx
1
import math
2
3
method = getattr(math, "sqrt")
4
result = method(16)
5
6
print(result) # 4.0
Output:
xxxxxxxxxx
1
4.0