0

In my python program, I have to call a function like this one:

driver_name.project.paired_config_L1_M_10g() 

What function I call depends on the value that I have stored in three variables, namely:

varA == M 
varB == L1 
varC = 10g 

varA, varB, and varC can have about 20 different values each, and I don't want to write a function call for each one of them (Albeit the actual function definition exists.) Typically I want to function call to be:

driver_name.project.paired_config_$varA_$varB_$varC() 

But this does not seem to work. Suggestions?

1
  • 3
    Does this object really have 8000 methods on it? It looks like these methods should just be 1 method that takes 3 arguments. Commented Apr 13, 2014 at 6:29

2 Answers 2

1

You can use getattr. It takes object and attribute name as a string and returns the attribute. Here the returned value is a method, that could be immediately called.

getattr(driver_name.project, 'project_config_%s_%s_%s' % (varA, varB, varC))()
Sign up to request clarification or add additional context in comments.

Comments

0

Have you considered a dictionary as one possible solution?

Just initialize the dictionary with the proper keys (varA,varB,varC) and the define functions. Then call it.

driver_func_dict['ML110g'] = driver_name.project.paired_config_M_L1_10g
#init more dictionary items here
...

#call function
driver_funct_dict[str(varA)+str(varB)+str(varC)]()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.