The main function may be this:
fn = getattr(foo_obj, str_command, None) if callable(fn): fn()
Of course, you should only allow specific methods:
str_command = ... #Double-check: only allowed methods and foo_obj must have it! allowed_commands = ['print', 'install', 'remove'] assert str_command in allowed_commands, "Command '%s' is not allowed"%str_command fn = getattr(foo_obj, str_command, None) assert callable(fn), "Command '%s' is invalid"%str_command #Ok, call it! fn()
source share