Learn to Code via Tutorials on Repl.it

← Back to all posts
*args and **kwargs in Python
LeonDoesCode (275)

Introduction

Python has a two awesome features that you may have seen in other languages. That is the *args and **kwargs arguments for functions. These arguments go at the end of the arguments in a function, like so:

def info(name, age, *args, **kwargs):

First, let's understand what *args and **kwargs mean. The first thing to mention is that the words args and kwargs are replaceable, they are just the standard ones as they explain the function of the two arguments. However, the terms *names and **info will still work, and can help to illustrate their use better.

This means that the only part of these arguments that matters, are the * and **. Although in Python, you do have to give them a identifier. So they have to have the args and kwargs or any other name. We are going to stick with the standards for this tutorial.

The second thing to know is that you don't have to use both. You can use either one by its self if you don't need the other one. But if you do include them, they have to be the last two arguments, with *args always coming before **kwargs.

The third thing to know is that they allow you to put in as many arguments as you want. So you might give a function with the *args argument, which will then allow you to put in as many arguments as you want when you call it.

*args

Firstly, we are going to define a function which uses *args:

# Make a write function with *args
def write(*args):
  # *args acts like a list of values, so we loop through each of them
  for arg in args:
    # We print out each arg in *args with a space after it
    print(arg, end=" ")
  # We print a new line after it has finished printing the rest
  print()

The main thing to know is that *args takes in values without a reference. This means that the function above takes in values like so:

test = [15, 7, 9.5]
write("hello", "there!", test, 100)
'''
Prints:
hello there! [15, 7, 9.5] 100
'''

As you can see, you can input any type of value you want, even variables. But there is no reference given to each argument.

*kwargs

Again, we will make a function which uses the **kwargs argument:

# Create a printVars function with **kwargs
def printVars(**kwargs):
  # **kwargs acts like a dictionary, so we loop all the items in it, taking all the keys and vals
  for key, val in kwargs.items():
    # Print out each key and val
    print(f"{key} = {val}")

Unlike *args, the **kwargs argument takes in values with a reference. This means that the function above would take in values like so:

info = ["married", "pilot", 12.7]
printVars(name="harry", age=46, info=info)
'''
Prints:
name = harry
age = 46
info = ['married', 'pilot', 12.7]
'''

Once again, you can input any value you want, as long as it comes with a reference, you can even set references to variables.

Conclusion

The best thing to do to grasp the idea of *args and **kwargs is to play around with them. Change things and try to make certain things happen. You'll soon understand how they work, and where you might use them.

Have a great day!

P.S
If you have any suggestions for tutorials, leave them in the comments and I'll be sure to have a look. If you like one in the comments, then give it an up vote to show that you want to see it. It makes my life so much more easier. Thanks in advance!