*args and **kwargs

Wojtek Walczak gminick at hacker.pl
Wed Nov 27 13:33:46 EST 2002


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.

-- 
[ ] 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