Passing parameters using **kargs

Mel Wilson mwilson at the-wire.com
Tue Jun 8 11:15:39 EDT 2004


In article <b4a8ffb6.0406071858.24dbb8ee at posting.google.com>,
tkpmep at hotmail.com (Thomas Philips) wrote:
>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?

I don't understand.  Isn't this it:

Python 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> kargs={'a':1, 'b':2, 'c':3}
>>> def r(a, b, c):
...     print a, b, c
...
>>> r(**kargs)
1 2 3


   Use of keyword arguments doesn't NEED to be part of the
function definition.

        Regards.        Mel.



More information about the Python-list mailing list