Passing a list of kwargs?

jwanga picture jwanga · Sep 30, 2009 · Viewed 70.5k times · Source

Can I pass a list of kwargs to a method for brevity? This is what i'm attempting to do:

def method(**kwargs):
    #do something

keywords = (keyword1 = 'foo', keyword2 = 'bar')
method(keywords)

Answer

Peter picture Peter · Sep 30, 2009

Yes. You do it like this:

def method(**kwargs):
  print kwargs

keywords = {'keyword1': 'foo', 'keyword2': 'bar'}
method(keyword1='foo', keyword2='bar')
method(**keywords)

Running this in Python confirms these produce identical results:

{'keyword2': 'bar', 'keyword1': 'foo'}
{'keyword2': 'bar', 'keyword1': 'foo'}