So basicly I'm trying to call functions within classes using strings. So normally if you create a class, you're able to call the functions as seen below:
class Example:
def __init__(self, name, address):
self.name = name
self.address = address
def test1(self):
print(self.name)
def test2(self):
print(self.address)
exm = Example("exm", 0x00)
exm.test1()
exm.test2()
This works fine ofcourse but I'm working on a bigger code using a lot of strings and I'm trying to call a function within a class using my strings. Say you have a variable with the string "test1" and then try to call the function as seen below:
input_user = "test1"
exm.input_user()
This doesn't work because Python doesn't recognize it as an attribute of the class. If I use a normal function that's not within a class, I would be able to call it using a string with the globals() function.
def test1():
print("Succes!")
func = "test1"
call = globals()[func]
call()
But if I try to use the same method to call my function within a class then I get a KeyError. Using the code below:
input_user = "exm.test1"
call = globals()[input_user]
call()
Does anyone have a solution for this? Any help would be greatly appreciated. Thank you in advance!