*Args and **Kwargs

Zachary Greenberg
3 min readSep 29, 2021
Image Source

Want to write a function, but have no idea how many parameters you’ll need? That is not a problem in Python thanks to the use of *args and **kwargs. When I first heard these terms thrown around, I honestly thought they were talking about tweedle-dee and tweedle-dum. It just sounded so silly at first. Now that I know about them, I can see how they can be very useful.

*args and **kwargs allow you to pass multiple arguments or keyword arguments to a function.’ — as explained by Davide Mastromatteo on realpython.com

This is a simple and effective explanation. They will allow you to pass in additional arguments to your functions. There is a difference between the two, the way I like to think of it is that *args are like the value of a variable, whereas *kwargs are like the key and value of a variable. I will show you what I mean.

*ARGS

In a simple function we do multiplication like this:

def multiply(x, y):
return x * y

The problem with this multiply function is that we can only do 2 numbers at a time. What if there were 3 or 4? That’s why *args comes in handy. We can put in as many numbers as we like.

def multiply(*args):
product = 1
for num in args:
product*=num
return product
print(multiply(2, 3, 7)) #42

With the function above, utilizing *args, we can enter in as many numbers as we want. For example if we put in the numbers 2, 3, 7, we would return 42. 42 is the product of these numbers. * is a technically a way to unpack variables, so we can actually think as if we are creating a list. That is why we have to loop through args.

**KWARGS

The difference between *args from now **kwargs, is the latter stands for keyword arguments. Like I said, we can think of this as if we are assigned a name (or key) to the variable (value). These are almost dictionary like. So, keeping that in mind, let’s say we want to keep track of the number of watches sold. Using **kwargs will allow us to store 2 pieces of information together, just like a dictionary. This is when we would utilize kwargs:

def watches_sold(**kwargs):
for brand, units_sold in kwargs.items():
print (str(brand) + ':' + str(units_sold))
watches_sold(rolex=3, citizen=5, movado=1, coach=2)#rolex:3
#citizen:5
#movado:1
#coach:2

Above I created a simple function that will allow us to track inventory, all we have to do is enter the keyword arguments and we will be able to know the brand of the watch as well as the number of units sold. It is important to note that the print statement is utilized in the function instead of a return otherwise the first argument would be the only one returned.

As you can see both *args and **kwargs can come in handy sometimes. It is important to also know that you can have BOTH *args and **kwargs together in a function. But you should also know that if you do, *args MUST come before **kwargs.

To sum up, *args and **kwargs are useful for when you don’t exactly know how many arguments will be passed through. For **kwargs, generally you’d use a print statement, and you can use both together as long as *args comes before *kwargs.

References:

Explanation of them — https://realpython.com/python-kwargs-and-args/

--

--