Member-only story

*args vs **kwargs in Python

Shobhit Agarwal
3 min readNov 9, 2023

--

In Python, *args and **kwargs are used to pass a variable number of non-keyword (positional) arguments and keyword arguments, respectively, to a function. They are often used when you need flexibility in the number of arguments a function can accept.

*args (Arbitrary Positional Arguments):

Think of *args as a way to pass multiple values to a function without naming them.

It allows you to pass as many values as you want, and the function collects them into a special list-like structure called a tuple.

This is useful when you’re not sure how many values you’ll need to pass.

Simple Python function to understand *args

Let’s try to understand the concept using a simple analogy or metaphor to make it more understandable:

Imagine you’re hosting a party, and you have a guest of honour, which is like arg1 in the function. This guest of honour is the most important person at the party. However, you also have a bunch of other friends (represented by *args) coming to the party. These friends are important too, but there can be many of them, and you want to be able to greet them all.

  • arg1 is like your guest of honour, the most important person.
  • *args is like your other friends who are also coming to the party.

--

--

Shobhit Agarwal
Shobhit Agarwal

Written by Shobhit Agarwal

🚀 Data Scientist | AI & ML | R&D 🤖 Generative AI | LLMs | Computer Vision ⚡ Deep Learning | Python 🔗 Let’s Connect: topmate.io/shobhit_agarwal

Responses (1)