[Tutor] How to pass varying number of arguments to functions called by a dictionary?
eryksun
eryksun at gmail.com
Wed Feb 11 15:44:53 CET 2015
On Wed, Feb 11, 2015 at 7:27 AM, boB Stepp <robertvstepp at gmail.com> wrote:
>
> 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.
Do it like this:
pass_args = {
'a': (x1, x2, x3),
'b': (y1, y2),
'c': (z,),
}
call_fcn[key_letter](*pass_args[key_letter])
Note the 'c' tuple is written as (z,). A comma is required to create a
tuple. Also note the call uses the "* expression" syntax to merge an
iterable with the call's positional arguments. For example, f(x0,
*(x1,x2,x3)) is equivalent to f(x0, x1, x2, x3). Refer to the glossary
definition of "argument", and for a more detailed discussion, see the
section on calls in the language reference.
https://docs.python.org/2/glossary.html#term-argument
https://docs.python.org/2/reference/expressions.html#calls
More information about the Tutor
mailing list