The world’s leading publication for data science, AI, and ML professionals.

Simplifying *args And *kwargs For Functions With Codes and Examples!

Understanding the complete concept of *args and *kwargs for python programming and machine learning.

Photo by AltumCode on Unsplash
Photo by AltumCode on Unsplash

Functions are an integral part of python and object-oriented programming.

Python being the elegant and simplistic language that it is offers the users a variety of options for easier and efficient coding.

One such concept is the inclusion of args and kwargs in python. These methods of passing a variable number of arguments to a function make the python programming language effective for complex problems.

The uses of using them correctly are numerous. These two parameters in functions will be extremely useful even in concepts of Machine Learning and deep learning while trying to construct your custom layers or functions for your respective projects.

In this article, we aim to understand these concepts intuitively with codes and examples. So, without further ado, let us dive in and start getting a better hold of these concepts.

Before we start Programming, let us revisit the topic of functions briefly.


What are Functions?

Functions are a block of code that is written in a program so that they can be recalled multiple times. The main utility of a function is so that it can be repeatedly called numerous times in the same program, and you don’t need to write the same codes over and over again. However, you can also use it to provide more structure and an overall better look to your programs.

Functions are defined using the keyword ‘def,’ which can be called with defined or undefined parameters. When you call the particular the particular function, then whatever the value is to be returned is interpreted by the Python compiler.

Below is the code block of a typical function –

def func(parameters if required):
   # code here
   return # solution

In Python, we can pass a variable number of arguments to a function using special symbols. The two possibilities for this are as follows:

  1. *args (Non-Keyword Arguments)
  2. **kwargs (Keyword Arguments)

Let us understand each of these concepts separately starting with *args and then moving on to **kwargs.


*args

The special syntax *args in function definitions in python is used to pass a variable number of arguments to a function. It is used to pass a non-key worded, variable-length argument list.

Why would you need this parameter option for your functions?

Let us assume we have 3 fruits in a basket, and you write a function with 3 variables and define it as follows:

And you will get the following output.

Apple Mango Banana

Everything looks good until you realize you also bought some grapes, and you want to add it as well to your function. You could add an extra variable and call it once again and continue this process. However, the more fruits you purchase, then you will have to keep updating over and over again. This is where the *args function comes into play.

You can simply perform this function as follows:

The syntax is to use the symbol to take in a variable number of arguments. By convention, it is often used with the word args. What args allows you to do is take in more arguments than the number of formal arguments that you previously defined.

This process is simple and effective. The concept of *args is exceptionally useful, as discussed in this section. Let us proceed to the **kwargs section and understand that concept in detail as well.


**kwargs

The special syntax **kwargs in function definitions in python is used to pass a keyworded, variable-length argument list. We use the name kwargs with the double star. The reason is that the double star allows us to pass through keyword arguments (and any number of them).

The **kwargs is a dictionary-like operation mapping the element values to their respective. Let us understand this concept with a similar example of fruits. Suppose you want to rank the fruits in the order they were placed or the order you prefer them the most. This process can be done as suggested from the below code block:

And you get the following output –

first Apple
second Mango
third Banana
fourth Grapes

A keyword argument is where you provide a name to the variable as you pass it into the function. One can think of the kwargs as being a dictionary that maps each keyword to the value that we pass alongside it. That is why when we iterate over the kwargs there doesn’t seem to be any order in which they were printed out.

For more complicated projects, you can utilize both the args and the kwargs function together for performing these tasks. I would highly recommend trying this out by yourself to gain a better understanding of these concepts.


Photo by Thought Catalog on Unsplash
Photo by Thought Catalog on Unsplash

Conclusion:

Summarizing what we discussed in this article, functions are an integral part of python and object-oriented programming. We had a brief introduction to python and a basic understanding of what exactly is the role of functions in the Python programming language.

The concepts of args and kwargs were discussed in detail and how exactly are these parameter choices useful in python programming. With the help of these topics, we can write a more efficient and productive code for processing complicated tasks.

If you have any queries concerning the topics covered today, then please do let me know about them in the comments below. I will try to get back to you as soon as possible. Feel free to let me know if I missed out on something. And you would like me to cover that in a future article.

Check out some of these other articles that might be useful for your programming journey!

Simple Fun Python Project For Halloween!

5+ Unique Python Modules For Creating Machine Learning and Data Science Projects That Stand out!

Understanding Advanced Functions In Python With Codes And Examples!

Thank you all for sticking on till the end. I hope you guys enjoyed reading this article. I wish you all have a wonderful day ahead!


References:

  1. Geek for Geeks
  2. YouTube

Related Articles