0

I was wondering if I could achieve something along these lines using python:

I have these 5 functions:

def compile_a(self):
    pass


def compile_b(self):
    pass


def compile_c(self):
    pass


def compile_d(self):
    pass


def compile_e(self):
    pass

and I have some function which is supposed to wrap these functions, in a sense like this:

def compile_by_command(self, command_type):
    if command_type == 'a':
        self.compile_a()
    elif command_type == 'b':
        self.compile_b()
    elif command_type == 'c':
        self.compile_c()
    elif command_type == 'd':
        self.compile_d()
    elif command_type == 'e':
        self.compile_e()

I was wondering if there is some pythonic magic which looks something like this:

command_pool = ['a', 'b', 'c', 'd', 'e']

def compile_by_command(self, command_type):
    if command_type in command_pool:
        self.compile_{command_type}()

I mean a way to call the function using string formatting or something of that sort.

Thanks.

eaRobust
  • 87
  • 6

0 Answers0