Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In python when passing arguments what does ** before an argument do? [duplicate]

Tags:

python

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())
like image 595
Ryan Detzel Avatar asked Jul 24 '09 18:07

Ryan Detzel


1 Answers

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
like image 69
sth Avatar answered Oct 22 '22 16:10

sth