Fix Python – Passing a list of kwargs?


Asked By – jwanga

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')

Now we will see solution for issue: Passing a list of kwargs?


Yes. You do it like this:

def method(**kwargs):
  print kwargs

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

Running this in Python confirms these produce identical results:

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

This question is answered By – Peter

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0