problems binding a dictionary

Dody Suria Wijaya dodysw at gmail.com
Sat Dec 10 15:38:53 EST 2005


vida00 at gmail.com wrote:
> this has to be a very silly thing.
> 
> I have a function foo taking a dictionary as parameters. i.e.: def
> foo(**kwargs): pass
> when I call foo(param1='blah',param2='bleh',param3='blih') everything
> is fine.
> but when I do:
>>>> def foo(**kwargs):
> ...     pass
> ...
>>>> d=dict(param1='blah',param2='bleh',param3='blih')
>>>> foo(d)
> 
> I get:
> 
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: foo() takes exactly 0 arguments (1 given)
> 
> Why? how do I pass the dictionary *d* to foo()?
> Thanks,
> 
> - Josh.
> 

simply because your parameter definition expect keyword parameter 
passing, and you are passing a paramater by placement. You should call 
the function like this:

f(mykey=d)

or, since d is dict, you could use d's key/vals as keyword parameter:
f(**d)

which equivalent to doing:
f(param1='blah',param2='bleh',param3='blih')


--
  dsw



More information about the Python-list mailing list