How to use *args and **kwargs in python?

Namitha gowda
Jan 14 · 3 min read

The *args and **kwargs in function definitions are used to pass a variable number of arguments to a function.

While programming, we may not be aware of all the possible use cases of our code and may want to use more options for the future, or for users interacting with the code. We can pass a variable number of arguments to a function by using *args and **kwargs in our code.

Understandings *args:

In python single asterisk is used to pass a variable number of arguments to a function and also used to pass a non-keyworded, variable-length argument list.

Here is an example which describes how to use *args and when to use *args .

Suppose we need a function enabling candidates to share their skills, but we don’t know in advance how many skill sets a given candidate will have:

Code:

def candidate_skills(*skills):
print("My skills: " + ", ".join(skills))

The above function now accepts one or more arguments:

Output :

candidate_skills('python', 'go')
My skills:python, go
candidate_skills('python', 'go', 'reactjs', 'vuejs')
My skills:python, go, reactjs, vuejs

Subsequently, we can also call our function by passing it a tuple, using the same asterisk syntax:

skill_sets = ('python', 'go', 'reactjs', 'vuejs')
candidate_skills(*skill_sets)
My skills:python, go, reactjs, vuejs

Understanding **kwargs:

In python double asterisks are used to pass a keyworded, variable-length argument list and we can think of the kwargs as being a dictionary that maps each keyword to the value that we pass alongside it.

Here is an example which describes how to use *kwargs and when to use *kwargs .

Now say we want a function enabling Candidates to share their details in various categories, but we don’t know in advance how many categories a given candidate will select:

def my_details(**details):
print("My details are...")
for category, detail in details.items():
print(f"{category}: {detail}")

The above function now accepts one or more keyword arguments:

Output:

my_details(qualification=’BE’, DOB=’24-04-1992’)
My details are…
qualification: BE
DOB: 24-04-1992
my_details(qualification=’MS’, DOB=’24-04-1994’,placeofbirth='karnataka', yearofpassing='2014')
My details are…
qualification: MS
DOB: 24-04-1994
placeofbirth: Bhadravathi
yearofpassing: 2014

We can also call our function by passing it a dictionary, using similar double asterisk syntax:

Output:

some_details = {"qualification"="MS", "DOB"="24-04-1994","placeofbirth"="karnataka", "yearofpassing"="2014"}
my_details(**some_details)
My details are…
qualification: MS
DOB: 24-04-1994
placeofbirth: Bhadravathi
yearofpassing: 2014

A function may be defined with a mixture of formal parameters, variable-length parameters, and variable-length keyword parameters. When doing so, they must appear in the definition in the following order:

Code:

def function_example(arg, *args, **kwargs):
...

Ordering

When ordering arguments within a function or function call, arguments need to occur in a particular order:

  1. Formal positional arguments
  2. *args
  3. Keyword arguments
  4. **kwargs

Conclusion

We can use the *args and **kwargs within a function definition in order to pass a variable number of arguments to the function.

Creating functions that accept *args and **kwargs are best used in situations where you expect that the number of inputs within the argument list will remain relatively small. The use of *args and **kwargs is primarily to provide readability and convenience, but should be done with care.

Note that argsand kwargsis just a name. You’re not required to use the name argsand kwargs. You can choose any name that you prefer.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade