-1

Here is what I would like to be able to do:

I have a file called functions, with lots of functions. The functions are all essentially the same, functionally speaking (i.e., they are all of the form: pandas.Dataframe -> pandas.Dataframe). Obviously, they do different things to the Dataframe, so in that sense they are different.

I'd like to be able to pass my main function a list of strings, which would be the actual function names in the module, and have my program translate the strings into function calls.

So, basically, instead of:

functions = [module.functionA, module.functionB, module.functionC]
x = g(functions) 
print(x)
> 'magical happiness'

I would have:

function_strings = ['functionA','functionB','functionC']
functions = interpret_strings_as_function_calls(module,function_strings)
x = g(functions)
print(x)
> 'magical happiness'

Is there a way to do this? Or do I need to write a function in the module that matches each string with it's corresponding function? i.e.:

def interpret_strings(function_string):
     if function_string == 'functionA':
            return module.functionA
     elif function_string == 'functionB':
            return module.functionB
     etc.

(or in a switch statement, or whatever)

Chris
  • 28,822
  • 27
  • 83
  • 158
  • Have you heard of dictionaries? – TigerhawkT3 Dec 14 '15 at 03:56
  • So I have to make a dictionary in my module? – Chris Dec 14 '15 at 03:57
  • Doesn't that seem like a hack to you? Kindof like doing the switch statement? – Chris Dec 14 '15 at 03:57
  • 1
    Try `f = dict(zip(function_strings, functions))` and see what you get. – TigerhawkT3 Dec 14 '15 at 03:58
  • ugh. did you read my question? because I am not sure you did... – Chris Dec 14 '15 at 03:58
  • No, it doesn't seem like a hack to me. It's actually [pretty accepted as a fine way to do it](http://stackoverflow.com/questions/1373164/how-do-i-do-variable-variables-in-python). – TigerhawkT3 Dec 14 '15 at 03:58
  • Ok bud: if I have a text file that specifies all the settings, then when I add a new function to my module, I have to add my function name to my dictionary, and reference my module dictionary from my code that is interpreting the text input. Just seems like the answer I accepted (or will accept) is the right way to do it... – Chris Dec 14 '15 at 04:01
  • Unless your algorithm is to get a complete listing of that module's functions and run them all (maybe with a regex to check their names or something, I don't know), you'd have to make an explicit reference of _some_ kind to any functions you want to use. – TigerhawkT3 Dec 14 '15 at 04:07
  • actually, I don't. if I use getattr, I access the dictionary that is already there, behind the scenes. Regardless, the alg is a learner and the functions are features...I will never use all the features in a given learner, but I need the flexibility to choose from my text file, rather than program each experiment. – Chris Dec 14 '15 at 04:10
  • "I'd like to be able to pass my main function a list of strings" - instead of passing a list of strings, pass a dictionary of `{string for name of function:reference to function, ...}`. I mean, you already have their names. – TigerhawkT3 Dec 14 '15 at 04:10
  • Also note that the answer you accepted is present in the linked duplicate as well. It's just not rated as highly as the dictionary version. – TigerhawkT3 Dec 14 '15 at 04:11
  • I would have the functions specified in a text file. Then I would read the text file in with my one file, say, runner.py. Independently, I would add or remove functions from my function file, functions.py, and modify my text settings file accordingly. I would never have a list or dictionary in my code...why would I zip the strings with the functions when I could just convert the string list to the function list? – Chris Dec 14 '15 at 04:15
  • What is with you fighting this? – Chris Dec 14 '15 at 04:15

1 Answers1

2

You can use getattr(module, function_string).

cdonts
  • 9,304
  • 4
  • 46
  • 72