Is there a way to convert a string into an API call?
something like this --
import externalLib
class InternalLib():
def function(param1='ext1'):
#so I want it to call externalLib.ext1
return externalLib.param1
Is there a way to convert a string into an API call?
something like this --
import externalLib
class InternalLib():
def function(param1='ext1'):
#so I want it to call externalLib.ext1
return externalLib.param1
You can use getattr
. Check out Calling a function of a module by using its name (a string) that might help you. :)
You'll want to use getattr
. Like so:
import externalLib
class InternalLib():
def function(param1='ext1'):
# Calls externalLib.ext1()
# Omit the final () for externalLib.ext1
return getattr(externalLib, param1)()
getattr(object, "attribute")
is the same as object.attribute
.