Difference between args and kwargs in Python

Introduction

I think the reason why this topic can be confusing specially to beginners is that it mixes different concepts together. It should not be hard to understand with proper context. In my opinion, to better understand the difference between Args and Kwargs in Python, the following points must be crystal clear…

  • Positional vs keyword arguments
  • Packing and unpacking
  • What is being sent vs what the function expects

Let us comment on each point…

  • A positional argument means the involved function expects an argument at a specific position for example: func(x, y, z) has x as the first argument, y as the second and z as the third. If we need to pass 1 for x, 2 for y and 3 for z then we have to call the function respecting the order like func(1, 2, 3). On the other hand, a keyword argument means we provide an argument name and value together when calling the function therefore the order is irrelevant. For example, we can call the function as func(z=3, x=1, y=2) and everything should work as expected. So, rule 1: be aware of what type of arguments you are passing
  • What about packing and unpacking? packing refers to combining individual positional arguments into a list or individual keyword arguments (i.e. key value pairs) into a dictionary. On the other hand, unpacking is the opposite operation in which we unwrap a list to individual values or a dictionary to key value pairs. Let us not overthink, it is as simple as that. Now syntax wise ! In Python *Args (you can use any argument name for example *x) and ** Kwargs are used for packing when we call a function (calling end) and unpacking in the function definition (receiving end). If you think about it ! there is nothing magical here. It is just a convenient language feature that provides flexibility. So rule 2: In your mind, imagine a list or dictionary being broken into values or key value pairs or the opposite operation where values or key value pairs are combined back to a list or dictionary.
  • The third rule that you need to put in mind is to be 100% sure about what you are sending to the function and what the function is expecting.

If you apply all these rules all together, there is going to be less chance for mistakes. Let us now dive into some examples…

Example code

We can call a function with multiple parameters using a list or tuple. Here is an example…

The function expects one argument of type list or tuple. Executing the code should print the following output…

A dictionary can also be used to pass multiple named parameters to a function. Here is an example…

The function expects one argument of type dictionary. Executing the code should print the following output…

Calling a function with positional and keyword arguments . Let us take an example…

Let us now define a function that uses *Args and **Kwargs…

To better understand what really happens is to know that unpacking happens at call time then in the function definition packing takes place. Keeping that in mind helps in tracking what values goes to what variables. Take the previous example:

Let us summarize…

Summary

  • Rule 1: be aware of what type of arguments you are passing
  • Rule 2: In your mind, imagine a list or dictionary being broken into values or key value pairs or the opposite operation where values or key value pairs are combined back to a list or dictionary
  • Rule 3: be sure what you are sending to a function and what the function is expecting

That is it for today. Thanks for visiting. Please use the comments section for feedback and questions.

Tags:

Add a Comment

Your email address will not be published. Required fields are marked *