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

Joel Goldstick joel.goldstick at gmail.com
Wed Feb 11 15:23:15 CET 2015


On Wed, Feb 11, 2015 at 8:27 AM, boB Stepp <robertvstepp at gmail.com> wrote:

> 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!
>
> First, its best to cut and paste your code along with the traceback for
the errors you got.

You might want to look up python *args and *kwargs.



> --
> boB
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>



-- 
Joel Goldstick
http://joelgoldstick.com


More information about the Tutor mailing list