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.
import math method = getattr(math, "sqrt") print(method(16)) # 4.0
In this example, we use
getattr() method to get the
sqrt method from
import math method = getattr(math, "sqrt") result = method(16) print(result) # 4.0