**kwargs
Matt Gushee
mgushee at havenrock.com
Tue Jan 18 19:49:24 EST 2000
sp00fD <sp00fD at yahoo.com> writes:
> Does **kwargs represent a dictionary? 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)
Ah, well, you've *sort of* got the right idea here.
> ? How would I do something like this ?
No need to. **kwargs is magic! You can do this:
my.foo(foo="something", bar="something else", food="spam",
thug="Dinsdale")
... and the keyword arguments 'food' and 'thug' are automatically
folded into a dictionary, so that *within* the my.foo() method:
kwargs['food'] => 'spam'
kwargs['thug'] => 'Dinsdale'
kwargs => {'food': 'spam', 'thug': 'Dinsdale'}
Note that the name 'kwargs' has no meaning for the Python interpreter
-- it's just a convention. The double asterisk is what works the
magic.
--
Matt Gushee
Portland, Maine, USA
mgushee at havenrock.com
http://www.havenrock.com/
More information about the Python-list
mailing list