Python args and kwargs


Variable length arguments

Sometimes, the number of arguments to be passed to the function are not fixed. For these cases, we have variable length arguments.

These are of two types:

  1. Variable length positional arguments(*args)
  2. Variable length keyword arguments(*kwargs)

*args – Variable length positional arguments

While working on the project we may come across a situation that we don’t know the number of arguments we want to pass to the function. So to solve this problem we have a provision in python to use an asterisk(*) symbol before the parameter name in the function, for example, *args. Most of the time you will see people use the “args” keyword but “args” is not the reserved keyword in python, we can use any name instead of “args” but use the asterisk(*) as a prefix.

Function with customer details as args.

In [1]:
def cust_details(*args):
    print(args)
In [2]:
cust_details("Steve",2680,'USA')
('Steve', 2680, 'USA')

Fetching values from *args.

In [3]:
def cust_details_value(*args):
    print("Customer Name =",args[0])
In [4]:
cust_details_value("Steve",2680,'USA')
Customer Name = Steve

Function using a random name other than args.

In [5]:
def emp_details(*employee):
    print("Employee Name =",employee[0])
In [6]:
cust_details("Gary",2780,'UK')
('Gary', 2780, 'UK')

**kwargs – Variable length keyword arguments

**kwargs is similar to *args but just with one difference. Let us try to understand with an example.

Consider that we have a function customer_details where we want to pass multiple arguments, but we are not sure how many arguments these can be, we are also not sure about the order in which they can be, so while processing those arguments it can be a mess because we don’t know that what each argument mean.

customer_details(“Tom”,”Cruise”,35,’USA’)

customer_details(“Will”,32,”USA”)

Now while processing these arguments it is going to be challenging that what does each argument means.

To fix this issue we use kwargs which stands for keyword arguments. Here we will define what does each argument means while passing to a function and in function, these will be treated as a dictionary. Here also “kwargs” is not a reserved keyword, we can use any name, we are just supposed to use a double asterisk(**) before the name.

Function with customer details as kwargs. It will return us dictionary.

In [1]:
def cust_details(**kwargs):
    print(kwargs)
In [2]:
cust_details(first_name="Steve",lasy_name="Grant",id=2680,Country='USA')
{'first_name': 'Steve', 'lasy_name': 'Grant', 'id': 2680, 'Country': 'USA'}

Fetching values of dictionary.

In [3]:
def cust_details_value(**kwargs):
    print("Last Name =",kwargs['last_name'])
In [4]:
cust_details_value(first_name="Steve",last_name="Grant",id=2680,Country='USA')
Last Name = Grant

Function using random name other than kwargs.

In [5]:
def emp_details(**employee):
    print("Last Name =",employee['last_name'])
In [6]:
emp_details(first_name="Steve",last_name="Grant",id=2680,Country='USA')
Last Name = Grant