Python kwargs get: The Complete Guide

In Python programming, a function can be used as reusable code that performs similar operations. But a function cannot take variable numbers of arguments, and because if you pass a variable number of arguments, it will throw TypeError.

If you define a function with three arguments and at the time of call, if you pass five arguments, then it will throw the TypeError.

To resolve the TypeError issue, we can either use *args or **kwargs. Let’s understand both in detail.

Understanding *args

To pass a variable number of arguments to a function in Python, use the *args (Non-Keyword Arguments). Python has *args, allowing us to pass a variable number of non-keyword arguments to a function.

We generally use *args as an argument when we are uncertain about the total number of arguments to pass in the function.

def multiplier(*num):
 multi = 1
 for i in num:
    multi = multi * i

 print("multiplier:", multi)

multiplier(1, 2)
multiplier(1, 2, 3, 4)
multiplier(1, 2, 3, 4, 5)


multiplier: 2
multiplier: 24
multiplier: 120

In this example, we pass *num as an argument representing variable-length arguments. For instance, we call the multiplier function the first time with two arguments, the second time with four arguments, and the third time with five arguments.

Inside the multiplier(*num) function, we used for loop, which multiplies the passed argument and prints the result.

Understanding **kwargs

To pass a variable-length keyword argument to a function in Python, use the **kwargs. To represent kwargs, we denote with a double asterisk ** before the parameter name.

The arguments are passed as a dictionary, and these arguments make a dictionary inside a function with a name same as the parameter excluding double asterisk **.

def spider_man(**arguments):
 print("\nData type of argument:",type(data))

 for key, value in data.items():
 print("{} is {}".format(key, value))

spider_man(Firstname="Peter", Lastname="Parker", Age=22, Phone=1234567890)
spider_man(Firstname="Peter 2", Lastname="Parker", Email="", Country="USA", Age=45, Phone=9876543210)
spider_man(Firstname="Peter 3", Lastname="Parker", Email="", Country="USA")


Data type of argument: <class 'dict'>
Firstname is Peter
Lastname is Parker
Age is 22
Phone is 1234567890

Data type of argument: <class 'dict'>
Firstname is Peter 2
Lastname is Parker
Email is
Country is USA
Age is 45
Phone is 9876543210

Data type of argument: <class 'dict'>
Firstname is Peter 3
Lastname is Parker
Email is
Country is USA

In the above code, we have a function spider_man() with **arguments as a parameter. We passed two dictionaries with variable argument length to the spider_man() function. Then, we used for loop inside spider_man() function, which works on the data of the given dictionary and prints the value of the dictionary.

To format the output in Python, we can use the str.format() method. The *args and **kwargs make the function more adjustable and flexible. The **kwargs has all the methods other dictionaries can have.

In this example, you can say that **kwargs will create a dictionary that is why it has a key and value. To get the value based on the key, you can use the get() method. 

Python kwargs get

To get the value associated with a specific key that does not exist in the Python dictionary, use the **kwargs get() method.

self.value = kwargs.get('value',"default value")

If you use a specific argument with a particular default value, you can write the following.

value = dict.get(key, default_when_key_is_missing)

We generally use the following code to get the value associated with a specific key in the Python dictionary.

value = dict[key]

But, trying to get a value for a key that doesn’t exist will throw an exception.

Fortunately, there is a **kwargs get() method, which we can use, and it is very convenient in this case.

If we prefer to assign the class attribute more traditionally, self.value = kwargs[‘value’], we have to confirm that the key age and a default value exist in kwargs before asking for it.

To add the needed keys to kwargs and set their default values, we can use the following code:

kwargs.setdefault('age', 'x')

That’s it for the Python kwargs get tutorial.

More posts

Python int() vs float()

Python floor float

Python print float

Leave a Comment