5) c. Packing Arbitrary Arguments

Note: This is an advanced topic. If you have absolutely no prior programming experience you can go ahead and skip this topic. If you skip it you’ll still be fine with your Python learning. This topic just introduces another way of doing something that you already know. Later after you’re done with the whole course and gained an all round knowledge of Python you can always come back an attempt this concept.



Sometimes we want a function to take an unknown number of similar arguments. For example, you can pass as many objects as you want into the print function.

In the following snippet we accept a collection of objects and we will print them one by one.

1
2
3
4
5
 Output:
1
2
3
4

This is almost what we want. We pass a tuple of numbers into the function and they are printed as expected. But we don’t want to pass a tuple. We want to pass numbers or other objects.

In Python you can use * to “pack” and “unpack” collections.

When you add an asterisk to the function definition, you pack all the incoming arguments into one tupple.



All we have to do is to add an asterisk before args in function definition and we get the result we wanted.

1
2
3
4
5
 Output:
1
2
3
4



When you call the function Python will understand that all of the arguments are actually elements of the args tuple.

As a convention whenever we put a * in the argument we normally put it as *args. This avoids any confusion later in the code because it makes it clear that args = with an “s” implies multiple arguments have been packed together. As you grow more familiar with this concept you will learn to immediately spot *args and relate that to it being a tuple.


You can also add other arguments into the function. But you have to understand that all of the positional arguments must come before the argument with variable length.

1
2
3
4
Output:
first arg=  1
all other arguments are a tuple (2, 3, 4)
<type 'tuple'>



Concept of **kwargs:

This next concept is a bit tricky and takes some time for newcomers to grasp. So try to hold on to this concept in your mind and try to keep going even when your heart feels unsteady. Don’t give up until you’re done with the quizzes and problems and I promise you that it will get much easier.

You’ve learnt that a single asterisk (*) packs all arguments into a tuple.

Well, double asterisk (**) packs the arguments into a dictionary!

It is denoted by **kwargs (short for “keyword args”) as a convention.

**kwargs works exactly like *args except that your function can only be called with keyword arguments. Here’s an example of what this means.

1
2
3
4
5
6
7
Output:
kwargs is a type of =
<type 'dict'>

Let's use kwargs like any other dictionary
key =  A value =  1
key =  B value =  2



Here’s what you’ve covered in this chapter:

funct( *args )funct( **kwargs )
All your arguments will become a tuple.All your arguments will become a dictionary.
Call function with or without keyword args. Both are valid:
funct(“yes”, “yes”)
funct(bacon = “yes”, eggs = “yes”)
Call function only with keyword args.
funct(bacon = “yes”, eggs = “yes”)
args[0], args[1]…kwargs.items()
kwargs.keys()
kwargs.values()



You would sooner or later ask why the python developers introduced these concepts. Couldn’t we just pass a tuple or a dictionary to a function and still get the job done?

The answer is “yes, we could”. But this concept makes using functions a tiny bit easier. This way, while calling the functions you don’t need to explicitly create a tuple / dictionary and pass it. You can just pass the values directly.

If you don’t get this concept, don’t worry. This is just another way of doing something you already know how. You’ll feel more comfortable absorbing it after you’ve mastered the basics completely.

Just try to remember this much and you should be good:

  • No asterisk : Normal argument
  • One asterisk : Tuple (*args)
  • Two asterisk : Dictionary (**kwargs)