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

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 :)

Dan



Wed, 23 Jul 2003 01:15:57 GMT  
 expanding a list and dict to a *args and **dict

Quote:

> 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:

Quote:
>>> def f(a,b,c):

...  print a,b,c
...
Quote:
>>> 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



Wed, 23 Jul 2003 01:21:55 GMT  
 expanding a list and dict to a *args and **dict

Quote:

> Is it possible to 'expand' a list or dictionary into its components to call
> a function expecting many parameters?

   Use builtin function "apply"! Read docs...

Oleg.
----

           Programmers don't die, they just GOSUB without RETURN.



Wed, 23 Jul 2003 01:39:33 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Q: List and Dicts as default args

2. Dict can't update with dict-like objects

3. NewBuiltins: added dict() -- inverse of dict.items()

4. Builtin dict should be callable, since a dict defines a funct ion

5. Builtin dict should be callable, since a dict defines a function

6. Dict to String and String to Dict with Visual Works

7. Passing dict-type arg list to 2nd func??

8. html forms, dicts and lists

9. Dict/List manipulation from C

10. list -> dict help

11. help w/assigning lists to dict

12. Two proposals concerning lists and dicts

 

 
Powered by phpBB® Forum Software