1

Here is function:

def runaction(ENV, action_list):

    appmanage_list = ['status']

    for action in action_list.keys():
        print('Action: %s' % action)

        if action in appmanage_list:

            from lib import appmanage_functions
            a = appmanage_functions.AppManage(ENV)
            a.[SOMETHING_GOES_HERE](ENV)

It's called like:

 runaction(env, main_f.opts2)

action_list (main_f.opts2 argument in function call) - is dictionary from argparse with {'optionname':True};

appmanage_list - will include all options, which need to be call with code above;

action - options name, same as method from appmanage_functions.AppManage class, need to be called.

In place of [SOMETHING_GOES_HERE] I want add action variable, to call appropriate method from class.

I tried something like:

(a. + action + (ENV))

and:

a.('%s' % action)(ENV)

But sure - this not works...

Python 2.7

setevoy
  • 3,637
  • 7
  • 41
  • 76

1 Answers1

2

object.foo can be rephrased as getattr(object, 'foo').

So you can say:

getattr(a, action)(ENV)
Kos
  • 64,918
  • 23
  • 156
  • 223