Is it possible to call functions in python based on string?
Lets say I have a string x="test_A;test_B;test_C"
I have a list sequence that I created based on the delimiter ";".
sequence = x.split(';')
print(sequence)
I get the following results
['test_A', test_B', 'test_C', '']
Now, I want to run the following functions in the order
- Test_A()
- Test_B()
- Test_C()
Is there any way in python to do that?
Try running this:
# Functions defined
def test_A(param):
print(param)
def test_B(param):
print(param + ' again')
def test_C(param):
print('and ' + param + ' again')
# Dictionary defined
d = dict({
'test_A': test_A,
'test_B': test_B,
'test_C': test_C
})
# Your code
x = "test_A;test_B;test_C"
sequence = x.split(';')
print(sequence)
for func_name in sequence:
d[func_name]('hi')
# Prints this
# hi
# hi again
# and hi again