0

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
hammies
  • 1,344
  • 2
  • 22
  • 46

2 Answers2

1

You can use getattr. Check out Calling a function of a module by using its name (a string) that might help you. :)

0

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.

wizzwizz4
  • 6,140
  • 2
  • 26
  • 62