[Tutor] **kw and self
Remco Gerlich
scarblac@pino.selwerd.nl
Thu, 21 Jun 2001 10:32:54 +0200
On 0, Willi Richert <w.richert@gmx.net> wrote:
> Why can't I just pass a dictionary? Why do I have to put ** before it?
Because then it would be passed as just a dictionary, a single argument.
Like
>>> def add(a, b): return a+b
>>> dict = {'a': 1, 'b': 2}
>>> add(dict)
TypeError: add() takes exactly 2 arguments (1 given)
It's the same as
>>> add(a = {'a':1, 'b': 2})
which is an error because 'b' gets no value.
>>> add(**dict)
3
You need the ** to tell Python you want to use the dictionary for keyword
arguments.
--
Remco Gerlich