*args and **kwargs

John Roth johnroth at ameritech.net
Wed Nov 27 14:27:43 EST 2002


"Wojtek Walczak" <gminick at hacker.pl> wrote in message
news:slrnauah8l.199.gminick at hannibal.localdomain...
> Dnia Wed, 27 Nov 2002 18:08:58 GMT, Dan napisa³(a):
> > thanks for the answer specifically for "args".  Still I don't quite
> > understand the Python syntax.
> >
> > *something = encapsulate into a set   ??
> > **something = encapsulate into a Dictionary??
> Yes, but only when defining a function.
> You can call a function in two ways (depends on a way it was defined):
>
> def first(**a):
>    print "\n".join([ "%s==%s" % (k,v) for k,v in a.items()])
>
> first(a="b",b="c",c="d")
>
>
> def second(a):
>    print "\n".join([ "%s==%s" % (k,v) for k,v in a.items()])
>
> second({"a":"b", "b":"c", "c":"d"})
>
> In first case you know, that you need to pass a dictionary from
> function's declaration (seeing **a just tells you that). In second
case,
> you don't, you need to take a look at code or documentation.
> Maybe not at all, but, for sure, in some way that strange construction
> is about readability.

I don't believe that's the case. What the * and ** constructions do
is give a name to a tuple and dictionary, respectively, that contain
extra positional and keyword parameters. Notice the word 'extra'
here, it's important.

On the calling side, they allow you to create a sequence and a
dictionary that contain the positional and keyword parameters. As one
of the posters says, it's a shortcut for the apply() built-in function,
and one that Guido prefers. He's said he wants to get rid of (or at
least depreciate) apply().

The fact that the function definition contains either * or ** has
nothing to do with how you call it.

John Roth
>
> --
> [ ] gminick (at) underground.org.pl  http://gminick.linuxsecurity.pl/
[ ]
> [ "Po prostu lubie poranna samotnosc, bo wtedy kawa smakuje
najlepiej." ]





More information about the Python-list mailing list