What is the purpose and use of **kwargs? [duplicate] What is the purpose and use of **kwargs? [duplicate] python python

What is the purpose and use of **kwargs? [duplicate]


You can use **kwargs to let your functions take an arbitrary number of keyword arguments ("kwargs" means "keyword arguments"):

>>> def print_keyword_args(**kwargs):...     # kwargs is a dict of the keyword args passed to the function...     for key, value in kwargs.iteritems():...         print "%s = %s" % (key, value)... >>> print_keyword_args(first_name="John", last_name="Doe")first_name = Johnlast_name = Doe

You can also use the **kwargs syntax when calling functions by constructing a dictionary of keyword arguments and passing it to your function:

>>> kwargs = {'first_name': 'Bobby', 'last_name': 'Smith'}>>> print_keyword_args(**kwargs)first_name = Bobbylast_name = Smith

The Python Tutorial contains a good explanation of how it works, along with some nice examples.

Python 3 update

For Python 3, instead of iteritems(), use items()


Unpacking dictionaries

** unpacks dictionaries.

This

func(a=1, b=2, c=3)

is the same as

args = {'a': 1, 'b': 2, 'c':3}func(**args)

It's useful if you have to construct parameters:

args = {'name': person.name}if hasattr(person, "address"):    args["address"] = person.addressfunc(**args)  # either expanded to func(name=person.name) or              #                    func(name=person.name, address=person.address)

Packing parameters of a function

  • Use .items() instead of .iteritems() for python 3
def setstyle(**styles):    for key, value in styles.iteritems():      # styles is a regular dictionary        setattr(someobject, key, value)

This lets you use the function like this:

setstyle(color="red", bold=False)

Notes

  • kwargs is variable name used for keyword arguments, another variable name can be used. The important part is that it's a dictionary and it's unpacked with the double asterisk operator **.
  • Other iterables are unpacked with the single asterisk operator *
  • To prevent confusion, it's probably best to stick with the recognized variable names, kwargs and args, for dictionaries and other iterables respectively.

Resources


kwargs is just a dictionary that is added to the parameters.

A dictionary can contain key, value pairs. And that are the kwargs. Ok, this is how.

The what for is not so simple.

For example (very hypothetical) you have an interface that just calls other routines to do the job:

def myDo(what, where, why):   if what == 'swim':      doSwim(where, why)   elif what == 'walk':      doWalk(where, why)   ...

Now you get a new method "drive":

elif what == 'drive':   doDrive(where, why, vehicle)

But wait a minute, there is a new parameter "vehicle" -- you did not know it before. Now you must add it to the signature of the myDo-function.

Here you can throw kwargs into play -- you just add kwargs to the signature:

def myDo(what, where, why, **kwargs):   if what == 'drive':      doDrive(where, why, **kwargs)   elif what == 'swim':      doSwim(where, why, **kwargs)

This way you don't need to change the signature of your interface function every time some of your called routines might change.

This is just one nice example you could find kwargs helpful.