Python Language *args and **kwargs Using **kwargs when writing functions

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

You can define a function that takes an arbitrary number of keyword (named) arguments by using the double star ** before a parameter name:

def print_kwargs(**kwargs):
    print(kwargs)

When calling the method, Python will construct a dictionary of all keyword arguments and make it available in the function body:

print_kwargs(a="two", b=3)
# prints: "{a: "two", b=3}"

Note that the **kwargs parameter in the function definition must always be the last parameter, and it will only match the arguments that were passed in after the previous ones.

def example(a, **kw):
    print kw

example(a=2, b=3, c=4) # => {'b': 3, 'c': 4}

Inside the function body, kwargs is manipulated in the same way as a dictionary; in order to access individual elements in kwargs you just loop through them as you would with a normal dictionary:

def print_kwargs(**kwargs):
    for key in kwargs:
        print("key = {0}, value = {1}".format(key, kwargs[key])) 

Now, calling print_kwargs(a="two", b=1) shows the following output:

print_kwargs(a = "two", b = 1)
key = a, value = "two"
key = b, value = 1


Got any Python Language Question?