Passing parameters using **kargs

Larry Bates lbates at swamisoft.com
Tue Jun 8 09:17:25 EDT 2004


Since kargs is a regular dictionary variable,
you can reference the variables you want with
kargs.get('<variablename>', None)

example

kargs.get('a', None) will return value of
keyword argument a or None if 'a' doesn't
exist.

If you are sure of what will exist you can just
use kargs['a'], but then you could more easily
do f(a=None, **kargs) in that case.

kargs.keys() will give you the names of the
keyword arguments if you want them.

No real reason to put them anywhere else that I
can see.

HTH,
Larry Bates



"Thomas Philips" <tkpmep at hotmail.com> wrote in message
news:b4a8ffb6.0406071858.24dbb8ee at posting.google.com...
> I want to access parameters that are passed into a function using the
> **kargs idiom. I define f(**kargs) via
>
> def f(**kargs):
>     print kargs
>     .
>     .
>
> the keyword arguments are converted to a dictionary, so that if I type
> f(a=1, b=2, c=3)
>
> the function prints
> {'a': 1, 'b': 2, 'c':3}
>
> Now assume the function has three variables a, b and c to which I want
> to assign the dictionary's values of 'a', 'b' and 'c'. How can I
> assign kargs['a'] to a, kargs['b'] to b, and kargs['c'] to c. Should I
> be trying to construct a string representation of each variable's name
> and using that as a key, or am I just thinking about this the wrong
> way?
>
> Thomas Philips





More information about the Python-list mailing list