Python Intermediate: How to use *args and **kwargs in Python

Table of Contents

Related Tutorials

A description of *args and **kwargs in Python

We’ll discuss the functions of the Python parameters ** (double star/asterisk) and * (single star/asterisk). We’ll also go over some Python args and kwargs examples here. Using certain symbols, we may supply a function with a variable amount of parameters.

Passing Arguments with Special Symbols

  • *args (Non-Keyword Arguments)
  • **kwargs (Keyword Arguments)

When we are unsure about the appropriate amount of arguments to provide to a function, we utilize the “wildcard” or “*” notation like this: *args OR **kwargs.

Python *args

In Python function definitions, the unusual syntax *args is used to send a variable number of arguments to a function. A variable-length, non-keyworded argument list is passed using it.

To accept a variable number of arguments, the syntax is to use the symbol *; by convention, it is commonly combined with the term args.

Example:

def test_function(*arg):
   for item in arg:
       print(item)
 test_function('EnableGeek', 'is', 'Awesome',)

Output:

EnableGeek
is
Awesome

You can accept additional arguments using *args than the number of formal arguments you previously declared. Any number of additional arguments can be added to your current formal parameters using *args (including zero extra arguments). The variable we associate with the * becomes iterable when we use the *, allowing us to perform higher-order functions like map and filter on it as well as other things like iterating over it.

Example:

def test_function(argument1, *args):
   print("First argument :", argument1)
   i = 0
   ind = ['Second','Third','Fourth']
   for item in args:
       print(f"{ind[i]} argument by *arg :", item)
       i+=1
 
test_function('Python', 'Java', 'Golang', 'Dart')

Output:

First argument : Python
Second argument by *arg : Java
Third argument by *arg : Golang
Fourth argument by *arg : Dart

Python **kwargs

Python function definitions can send a keyworded, variable-length argument list by using the unique syntax **kwargs. We refer to them by their double star and the name kwargs. Because of the double star, we are able to ignore keyword arguments. An example of *kwargs for a variable number of keyword arguments is provided by a Python program.

Example:

def test_function(**kwargs):
   for key, value in kwargs.items():
       print("%s : %s" % (key, value))
 
test_function(first='Enablegeek', second='is', third='Awesome')
 

Output:

first : Enablegeek
second : is
third : Awesome

A keyword argument is used to give the variable a name before passing it to the function.

The kwargs may be viewed as a dictionary that associates each term with the value that is passed along with it. Because of this, there doesn’t appear to be any sequence in which the kwargs were printed out when we iterate through them.

A Python program that uses one more parameter to show **kwargs for a variable number of keyword arguments.

Example:

def test_function(argument1, **kwargs):
   for key, value in kwargs.items():
       print("%s : %s" % (key, value))
   print(argument1)
  
test_function("Good morning!", first='Enablegeek', second='is', third='Awesome')
 

Output:

first : Enablegeek
second : is
third : Awesome
Good morning!

Using both *args and **kwargs

In this case, we are sending *args and **kwargs as arguments to the test_function function, and after passing all the data, we are outputting it in lines.

Example:

def test_function(argument1, argument2, argument3):
   print("argument1:", argument1)
   print("argument2:", argument2)
   print("argument3:", argument3)
 
 
args = ("Enablegeek", "is", "Awesome")
test_function(*args)
 
kwargs = {"argument1": "Enablegeek", "argument2": "is", "argument3": "Awesome"}
test_function(**kwargs)
 

Output:

argument1: Enablegeek
argument2: is
argument3: Awesome
argument1: Enablegeek
argument2: is
argument3: Awesome

In this case, we are sending the arguments *args and **kwargs to the test_function function. where first=’Enablegeek’, second=’is’, third=’Awesome’ are supplied as **kwargs and printed on the same line, and “geeks,” “for,” and “geeks” are passed as *args.

Example:

def test_function(*args, **kwargs):
   print("args: ", args)
   print("kwargs: ", kwargs)
 
test_function('Enablegeek', 'is', 'Awesome', first='Enablegeek', second='is', third='Awesome')
Output:
args:  ('Enablegeek', 'is', 'Awesome')
kwargs:  {'first': 'Enablegeek', 'second': 'is', 'third': 'Awesome'}

Output:

args:  ('Enablegeek', 'is', 'Awesome')
kwargs:  {'first': 'Enablegeek', 'second': 'is', 'third': 'Awesome'}

If you want to know anything detailed about this topic, you can learn it from Python’s Official Documentation.

Share The Tutorial With Your Friends
Facebook
Twitter
LinkedIn
Email
WhatsApp
Skype