tags:

views:

210

answers:

4

Possible Duplicate:
What does *args and **kwargs mean?

From reading this example and from my slim knowledge of Python it must be a shortcut for converting an array to a dictionary or something?

class hello:
    def GET(self, name):
        return render.hello(name=name)
        # Another way:
        #return render.hello(**locals())
+1  A: 

It "unpacks" an dictionary as an argument list. ie:

def somefunction(keyword1, anotherkeyword):
   pass

it could be called as

somefunction(keyword1=something, anotherkeyword=something)
or as
di = {'keyword1' : 'something', anotherkeyword : 'something'}
somefunction(**di)
Silfverstrom
+6  A: 

In python f(**d) passes the values in the dictionary d as keyword parameters to the function f. Similarly f(*a) passes the values from the array a as positional parameters.

As an example:

def f(count, msg):
  for i in range(count):
    print msg

Calling this function with **d or *a:

>>> d = {'count': 2, 'msg': "abc"}
>>> f(**d)
abc
abc
>>> a = [1, "xyz"]
>>> f(*a)
xyz
sth
+1  A: 

From the Python docuemntation, 5.3.4:

If any keyword argument does not correspond to a formal parameter name, a TypeError exception is raised, unless a formal parameter using the syntax **identifier is present; in this case, that formal parameter receives a dictionary containing the excess keyword arguments (using the keywords as keys and the argument values as corresponding values), or a (new) empty dictionary if there were no excess keyword arguments.

This is also used for the power operator, in a different context.

Reed Copsey
+1  A: 

**local() passes the dictionary corresponding to the local namespace of the caller. When passing a function with ** a dictionary is passed, this allows variable length argument lists.

jradakov