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

boB Stepp robertvstepp at gmail.com
Wed Feb 11 16:16:40 CET 2015


On Wed, Feb 11, 2015 at 8:44 AM, eryksun <eryksun at gmail.com> wrote:
> 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.

I was trying to puzzle out this "* expression" syntax from "Learning
Python, 4th Ed." from a more general table of function
argument-matching forms (author's table 18-1) when your post arrived.
Your explanation cleared things up immediately. Thanks!


-- 
boB


More information about the Tutor mailing list