expanding a list and dict to a *args and **dict

Michael Hudson mwh21 at cam.ac.uk
Fri Feb 2 18:21:55 CET 2001


Dan Parisien <dan at eevolved.com> writes:

> Is it possible to 'expand' a list or dictionary into its components to call 
> a function expecting many parameters?
> 
> I want to be able to call a function without accessing each individual 
> element manually (list[0], list[1], list[2])
> 
> I'm sending random parameters over a network (*args and **dict). When I 
> unpickle them on the other side, I get a python list and dictionary. I then 
> call an arbitrary function (actually a reference to a function that was 
> bound at runtime a la 
> self.randomfunction = obj.specificfunction
> )
> 
> That function is not expecting a list and a dictionary, instead it is 
> expecting specific parameters(that i don't know at runtime) and I want to 
> take the list and turn it into a bunch of parameters like list[0], list[1], 
> list[2] (but as stated above, I can't do that)
> 
> Ha. I hope someone understood me :)

Only a bit, but do you know about apply (a builtin function) and/or
this sort of thing:

>>> def f(a,b,c):
...  print a,b,c
... 
>>> f(*(1,2),**{'c':3})
1 2 3

I *think* that's what your asking for...

Cheers,
M.

-- 
  I have gathered a posie of other men's flowers, and nothing but the
  thread that binds them is my own.                       -- Montaigne



More information about the Python-list mailing list