args and kwargs in Python

args and kwargs in Python

Introduction

In this article, we'll discuss *args and **kwargs in Python with their uses and examples.

When writing a function, we often need to pass values to the function. These values are called function arguments.

Problem with Function Arguments

Let us define a function to add two numbers in Python. We'll write it as:

def add(x,y):
    return x+y

print(add(2,3))

Output:

5

What if further it is required to add three numbers? Simple, we can modify the function to accept three arguments and return their sum as:

def add(x, y, z):
    return x+y+z

print(add(2, 3, 5))

Output:

10

Wasn't that quite simple? Yes, it was!

But what if we're again required to add two numbers only? Will our modified function help us get the sum? Let's see:

def add(x, y, z):
    return x+y+z


print(add(2, 3))

Output:

Traceback (most recent call last):
  File "D:\Quarantine\Test\Blog-Codes\args-kwargs\main.py", line 14, in <module>
    print(add(2, 3))
TypeError: add() missing 1 required positional argument: 'z'

You see the problem?

The problem arises when we have a variable number of arguments. Shall we keep modifying the function to accept the exact number of arguments? Of course not, we won't be doing this.

Thus, there must be some other way to do it. This is where *args and **kwargs jump in!

*args and **kwargs are used as arguments of a function when we are unsure about the number of arguments to pass in the functions.

*args in Python

*args allows us to pass a variable number of non-keyword arguments to a Python function. In the function, we should use an asterisk(*) before the parameter name to pass a variable number of arguments.

def add(*args):
    print(args, type(args))

add(2, 3)

Output:

(2, 3) <class 'tuple'>

Thus, we're sure that these passed arguments make a tuple inside the function with the same name as the parameter excluding *.

Now let us rewrite our add() function with a variable number of arguments.

def add(*numbers):
    total = 0
    for num in numbers:
        total += num
    return total


print(add(2, 3))
print(add(2, 3, 5))
print(add(2, 3, 5, 7))
print(add(2, 3, 5, 7, 9))

Output:

5
10
17
26

Note that the name of the argument need not necessarily be args, it can be anything, in this case, it's numbers. But it's generally a standard way to use *args as the name.

kwargs in Python

kwargs allows us to pass a variable number of keyword arguments to a Python function. In the function, we use the double-asterisk (``)before the parameter name to denote this type of argument.

def total_fruits(**kwargs):
    print(kwargs, type(kwargs))


total_fruits(banana=5, mango=7, apple=8)

Output:

{'banana': 5, 'mango': 7, 'apple': 8} <class 'dict'>

Thus we see that the arguments, in this case, are passed as a dictionary and these arguments make a dictionary inside the function with name same as the parameter excluding ``.

Now, let us complete the total_fruits() function to return the total number of fruits.

def total_fruits(**fruits):
    total = 0
    for amount in fruits.values():
        total += amount
    return total


print(total_fruits(banana=5, mango=7, apple=8))
print(total_fruits(banana=5, mango=7, apple=8, oranges=10))
print(total_fruits(banana=5, mango=7))

Output:

20
30
12

Note that the name of the argument need not necessarily be kwargs, it can be anything, in this case, it's fruits. But it's generally a standard way to use **kwargs as the name.

Conclusion

In this blog, we learned about two special keywords in Python - *args and **kwargs that make a Python function flexible to accept a variable number of arguments and keyword arguments respectively.

Thanks for reading!

You can find the code for this blog here.

Did you find this article valuable?

Support Ashutosh Writes by becoming a sponsor. Any amount is appreciated!