**kwargs

Gordon McMillan gmcm at hypernet.com
Tue Jan 18 17:50:38 EST 2000


sp00fD writes:

> Does **kwargs represent a dictionary?  

Yes.

> If so, can I have a method like
> such
> 
> def foo(self, foo=None, bar=None, **kwargs)
> 
> and then do something like
> 
> this_dict = {}
> ..insert stuff into this_dict..
> 
> my.foo(foo="something", bar="something else", this_dict)

Not quite. To call foo with the named args wrapped up as a 
dictionary, you need to use apply:
  apply(foo, ("something", "something else"), this_dict)
or
  this_dict['foo'] = "something"
  this_dict['bar'] = "something else"
  apply(foo, (), this_dict)
 
Note that foo cannot tell which of the above ways was used, 
(ie, "foo" and "bar" are not in kwargs when foo examines it).

BTW, while foo-abuse is common, I've never seen it carried to 
such extremes.

- Gordon




More information about the Python-list mailing list