Newbie question: How do I add elements to **kwargs in a function?

Aaron Garrett aaron.lee.garrett at gmail.com
Tue Mar 17 04:24:18 CET 2009


On Mar 16, 9:59 pm, Chris Rebert <c... at rebertia.com> wrote:
> On Mon, Mar 16, 2009 at 7:48 PM, Aaron Garrett
>
>
>
> <aaron.lee.garr... at gmail.com> wrote:
> > I have spent quite a bit of time trying to find the answer on this
> > group, but I've been unsuccessful. Here is what I'd like to be able to
> > do:
>
> > def A(**kwargs):
> >    kwargs['eggs'] = 1
>
> > def B(**kwargs):
> >    print(kwargs)
>
> > def C(**kwargs):
> >    A(**kwargs)
> >    B(**kwargs)
>
> > I'd like to be able to make a call like:
>
> > C(spam=0)
>
> > and have it print
> > {'spam': 0, 'eggs': 1}
>
> > But it doesn't do that. Instead, it gives
> > {'spam': 0}
>
> > I was under the impression that kwargs is passed by reference and,
>
> It's not. Semantically, the dictionary broken up into individual
> keyword arguments on the calling side, and then on the callee side, a
> fresh dictionary is created from the individual arguments. So while
> Python uses call-by-object, extra packing/unpacking takes place in
> this case, causing copying, and thus your problem.
>
> Cheers,
> Chris
>
> --
> I have a blog:http://blog.rebertia.com


Thank you for the explanation.

Aaron



More information about the Python-list mailing list