[BangPypers] function parameter query
Dhruv Baldawa
dhruvbaldawa at gmail.com
Wed Jan 23 09:39:04 CET 2013
On Wed, Jan 23, 2013 at 1:55 PM, Nitin Kumar <nitin.nitp at gmail.com> wrote:
> Can anyone explain me why d (2nd parameter of function show) is getting set
> to 3, when I am not passing any value to it and kwargs contains d=3.
>
> I think you mean 4 over here
> Why kwargs value is getting set to d argument. Is this the
> expected behavior?
>
And yes, this is the expected behaviour. When you have a dictionary (kwargs
in this case), and you use **dictionary in a function call, it "unpacks"
the dictionary into the parameters and makes the call.
For example, if you have:
d = {'a': 1, 'b': 2, 'c': 3}
def foo(a, b, c):
print a, b, c
then
foo(**d) is equivalent to calling foo(a=1, b=2, c=3)
in your test() function above, what you are doing by show(c, **kwargs)
is show(c,
d=4, e=5)
--
Dhruv Baldawa
(http://www.dhruvb.com)
More information about the BangPypers
mailing list