[Tutor] How to pass varying number of arguments to functions called by a dictionary?

boB Stepp robertvstepp at gmail.com
Wed Feb 11 14:27:30 CET 2015


Python 2.4.4, Solaris 10

I have a file of functions. Based on what is read in a data file,
different functions in the file of functions will need to be called. I
have been trying to make the following approach work, so far
unsuccessfully as, in general, each function may have a different
number of arguments that might have to be passed to it.

def func1(x1, x2, x3):
    pass

def func2(y1, y2):
    pass

def func3(z):
    pass

call_fcn = {'a': func1, 'b': func2, 'c': func3}

call_fcn[key_letter](???)

How can I successfully pass the needed arguments needed for each
possible function with this approach? I naively tried to do something
like:

pass_args = {'a': (x1, x2, x3), 'b': (y1, y2), 'c': (z)}
call_fcn[key_letter](key_letter)

But ran into the syntax error that I was giving one argument when
(possibly) multiple arguments are expected.

Is what I am trying to do a viable approach that can be made to work?
Otherwise, I will brute-force my way through with if-elif-else
statements.

Thanks!

-- 
boB


More information about the Tutor mailing list