I have a class with lots of methods whose names follow a pattern:
convert_to_A(self)
, convert_to_B(self)
, convert_to_C(self)
, ...
The user is going to provide me a list of functions to call. Say something like -
user_options = ["A", "G", "K", ...]
and I just stumbled upon the awesome globals()
, locals()
functions which I think are going to solve my problem. So a method inside the function which calls them through locals()
should work right? -
def call_user_options(self):
for step in self.user_options:
locals()["convert_to_" + step]()
But it didn't. Also tried calling locals()["self.convert_to_" + step]()
but it was of no avail. Printing locals()
and globals()
gives this -
locals = {'self': <__main__.test instance at 0x105466200>}
globals = {'__builtins__': <module '__builtin__' (built-in)>, '__file__': 'globals_locals.py', '__package__': None, 'test': <class __main__.test at 0x105429ae0>, '__name__': '__main__', 'main': <function main at 0x10544bed8>, '__doc__': None}
So how do I exactly call those methods from a method inside the class when they're hidden behind self
?