Many new python geeks have a hard time figuring out the *args and **kwargs magic variables when working with them for the first time, but they are not as hard as they seem. There are many questions that come up in your mind right now such as: what are *args and **kwargs in python, why do we need them and how can we use them in practical examples.

The purpose of this tutorial is to help python beginners understand or make a better understanding of python magic variables such as *args and **kwargs through some basic genuine practical examples.

How To Use *args

First, what is the purpose of *args? As many of you may know *args are used to help the developer passing a variable number of arguments to a function. The term variable is very important here as it basically is the key to understand the role of *args in the python programming language.

The best way to learn new things is to take a practical approach of the things you want to learn. Lets take a simple example and think or assume that you want to build a multiply function for your maths teacher. The multiply function will take a number of arguments, will multiply them and print the result at the end. But, you don’t know the number of arguments this function will take because your teacher told you to be variable. So how do you solve this problem in a pythonic way?

Not very hard if you already know about *args. You define the function multiply as shown below and pass *args inside it. This will tell the function to accept a number of arguments that varies based on the user.

def multiply(*args): pass

But before building our multiply function open a new python interpreter shell and run the following code. I use ipython as my working python shell and it has been my favorite so far, but everything is up to you. You can use the default python shell.

def echo_args(*args): for arg in args: print arg

The symbol * makes args iterable which means that you can run a for loop and process every argument inside the args list. For example if you use pass 1,2,3 as arguments in your above function echo_args you will get them printed on your console like shown below.

In [20]: echo_args(1,2,3) 1 2 3

Now that I think you python nerds have a clear understanding of *args it is time to finish our work with the multiply function. I will import the mul operator and reduce from functools.

from operator import mul from functools import reduce def multiply(*args): ....return reduce(mul, args)

Note: mul and reduce have nothing to do with *args, they are tools that help us to build our function. Read more about mul operator and reduce function in google if you haven’t seen them before being used in a python program.

In simple words *args is used in cases when you don’t know how many arguments are going to be passed to the function by the user.

def get_arguments(a, *args): ....: print "The first argument is ", a ....: for arg in args: ....: print "This argument came from args:", arg

The following is the usage of the above function get_arguments.

In [28]: get_arguments(13, 13, 13) The first argument is 13 This argument came from args: 13 This argument came from args: 13

Try the this now. Create a variable called elements and make a tuple with some elements in it like shown below.

elements = (1, 2, 3)

Then pass elements to the function get_arguments and run it.

In [30]: get_arguments(elements) The first argument is (1, 2, 3)

What do you see? Did you expect anything else? As you can see from the above output of the get_arguments function a is assigned to the elements when the user passes elements in to the function.

What happens if you do the following?

get_arguments(*elements)

You will get the following output.

The first argument is 1 This argument came from args: 2 This argument came from args: 3

The * unpacks the tuple elements so it is the same as doing get_arguments(1,2,3). Try it on your console and I bet there will be no difference.

In [32]: get_arguments(1,2, 3)

And the following is the output from running the function get_arguments with 1,2,3 as its arguments.

The first argument is 1 This argument came from args: 2 This argument came from args: 3

I hope to have helped you learning and getting to know the *args python magic variable. I am very sure that the examples given here are very basic, but very helpful for beginners. In the next part we will learn about the **kwargs magic variable in python.