Python **kwargs

Python **kwargs allows function call to pass variable number of keyword (named) arguments to the function.

The datatype of kwargs is dictionary. So, keywords and respective argument values come as key:value pairs. The number of key:value pairs in kwargs is determined only by the function call at the runtime.

Double asterisk ** before kwargs is the unpacking operator. It unpacks the arguments passed to the function, as dictionary.

In this tutorial, we will learn how to use **kwargs in a function definition to accept any number of named arguments to the function.

Example: Python **kwargs

In this example, we will demonstrate a simple use case, just what **kwargs do. We will create a function that can accept any number of keyword arguments.

Python Program

def myFunction(**kwargs):
    for kw in kwargs:
        print(kw, '-', kwargs[kw])

if __name__ == "__main__":
    myFunction(a = 24, b = 87, c = 3, d = 46)
Run Code Copy

Output

a - 24
b - 87
c - 3
d - 46

We are calling myFunction() with four named arguments, namely a, b, c and d.

In myFunction() definition, we used kwargs parameter with ** unpacking operator, to accept any number of named arguments.

In the first named argument a = 24, a is the keyword and 24 is the argument value. Therefore, a goes as key and 24 goes as value. In the function, a and 24 can be accessed as key:value pair from the dictionary kwargs. The same explanation holds for the remaining named arguments passed to the function.

kwargs is parameter name

kwargs is just a name that we have given to the parameter that can accept any number of named arguments. You can use any name instead of kwargs, that fits your requirement.

In the following example, program, we will use a different name, say computers, instead of kwargs for keyword arguments in myFunction() function definition.

Python Program

def myFunction(**computers):
    for kw in computers:
        print(kw, '-', computers[kw])

if __name__ == "__main__":
    myFunction(dell = 1299.50, asus = 1870.00, hp = 1990.50)
Run Code Copy

Output

dell - 1299.5
asus - 1870.0
hp - 1990.5

kwargs is a Dictionary

We have already learnt that the datatype of kwargs is dictionary. So, you can use all the dictionary methods on kwargs.

For example, we will use dict.items() method, to iterate over keys and values simultaneoulsy.

Python Program

def myFunction(**kwargs):
    for key, value in kwargs.items():
        print(key, '-', value)

if __name__ == "__main__":
    myFunction(a = 24, b = 87, c = 3, d = 46)
Run Code Copy

Output

a - 24
b - 87
c - 3
d - 46

**kwargs with Other Parameters

Just a function can have multiple parameters in its definition, we can also have parameters along with **kwargs. Please remember that **kwargs is just another parameter. The only difference of **kwargs from other parameters is that, it can take multiple named arguments.

In the following example, we will define a function myFunction(), with parameters x, y and **kwargs.

Python Program

def myFunction(x, y, **kwargs):
    print(x)
    print(y)
    for key, value in kwargs.items():
        print(key, '-', value)

if __name__ == "__main__":
    myFunction("ABC", "MNO", a = 24, b = 87, c = 3, d = 46)
Run Code Copy

Output

ABC
MNO
a - 24
b - 87
c - 3
d - 46

**kwargs with *args

While **kwargs can accept any number of named arguments, Python *args can accept any number of positional arguments.

You can use *args and **kwargs in a function definition to accept both positional arguments and named arguments, whose count is unknown.

In the following example, we will define a function with both *args and **kwargs.

Python Program

def myFunction(*args, **kwargs):
    print(args)
    print(kwargs)

if __name__ == "__main__":
    myFunction("hello", "mars", a = 24, b = 87, c = 3, d = 46)
Run Code Copy

Output

('hello', 'mars')
{'a': 24, 'b': 87, 'c': 3, 'd': 46}

Just to remind, the datatype of args is tuple, and the datatype of kwargs is dictionary.

Summary

In this tutorial of Python Examples, we learned how to use **kwargs to accept any number of named arguments in a function.

Related Tutorials

Code copied to clipboard successfully 👍