18

Possible Duplicate:
What does ** and * do for python parameters?
What does *args and **kwargs mean?

Simple program:

storyFormat = """                                       
Once upon a time, deep in an ancient jungle,
there lived a {animal}.  This {animal}
liked to eat {food}, but the jungle had
very little {food} to offer.  One day, an
explorer found the {animal} and discovered
it liked {food}.  The explorer took the
{animal} back to {city}, where it could
eat as much {food} as it wanted.  However,
the {animal} became homesick, so the
explorer brought it back to the jungle,
leaving a large supply of {food}.

The End
"""                                                 

def tellStory():                                     
    userPicks = dict()                              
    addPick('animal', userPicks)            
    addPick('food', userPicks)            
    addPick('city', userPicks)            
    story = storyFormat.format(**userPicks)
    print(story)

def addPick(cue, dictionary):
    '''Prompt for a user response using the cue string,
    and place the cue-response pair in the dictionary.
    '''
    prompt = 'Enter an example for ' + cue + ': '
    response = input(prompt).strip() # 3.2 Windows bug fix
    dictionary[cue] = response                                                             

tellStory()                                         
input("Press Enter to end the program.")     

Focus on this line:

    story = storyFormat.format(**userPicks)

What does the ** mean? Why not just pass a plain userPicks?

Community
  • 1
  • 1
DNB5brims
  • 27,282
  • 47
  • 125
  • 188

2 Answers2

43

'**' takes a dict and extracts its contents and passes them as parameters to a function. Take this function for example:

def func(a=1, b=2, c=3):
   print a
   print b
   print b

Now normally you could call this function like this:

func(1, 2, 3)

But you can also populate a dictionary with those parameters stored like so:

params = {'a': 2, 'b': 3, 'c': 4}

Now you can pass this to the function:

func(**params)

Sometimes you'll see this format in function definitions:

def func(*args, **kwargs):
   ...

*args extracts positional parameters and **kwargs extract keyword parameters.

Manny D
  • 19,402
  • 2
  • 27
  • 31
  • So, it can take the dictionary key to map back the params, is that right? What is this function/feature called? I found it is very funny and powerful, is this unique for the python ONLY? – DNB5brims Nov 06 '11 at 15:55
  • @DNB5brims, I believe this is called "destructuring". It is finding its way into other languages such as Javascript (ECMAScript 2015). – johnsimer May 23 '16 at 19:57
  • 1
    Note that the key values of the `params` dict must match the names of the optional parameters listed in the definition of `func`, otherwise a `TypeError` will occur. – Minh Tran Mar 09 '18 at 09:48
4

**means kwargs. here is a good article about it.
read this: http://www.saltycrane.com/blog/2008/01/how-to-use-args-and-kwargs-in-python/

marxus
  • 452
  • 2
  • 6