problems binding a dictionary

V

vida00

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:.... pass
....
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.
 
V

vida00

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

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.

I mean, short of defining as foo(*args), or foo(dict).
 
S

Sam Pointon

You're not 'exploding' the dict to the param1='blah' etc form - you-re
actually passing it in as a single dict object. To solve this, add a **
to the front of a dict you want to explode in a function, just as you'd
add a * to explode a sequence.
 
D

Dody Suria Wijaya

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top