Python *args and **kwargs

In programming, we define a function to make a reusable code that performs similar operation. To perform that operation, we call a function with the specific value, this value is called a function argument in Python.

We would recommend you to read Python Function and Python Function Arguments.

Suppose, we define a function for addition of 3 numbers.

Example 1: Function to add 3 numbers

def adder(x,y,z):
    print("sum:",x+y+z)

adder(10,12,13)

When we run the above program, the output will be

sum: 35

In above program we have adder() function with three arguments x, y and z. When we pass three values while calling adder() function, we get sum of the 3 numbers as the output.


Lets see what happens when we pass more than 3 arguments in the adder() function.

def adder(x,y,z):
    print("sum:",x+y+z)

adder(5,10,15,20,25)

When we run the above program, the output will be

TypeError: adder() takes 3 positional arguments but 5 were given

In the above program, we passed 5 arguments to the adder() function instead of 3 arguments due to which we got TypeError.


Introduction to *args and **kwargs in Python

In Python, we can pass a variable number of arguments to a function using special symbols. There are two special symbols:

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

We use *args and **kwargs as an argument when we are unsure about the number of arguments to pass in the functions.


Python *args

As in the above example we are not sure about the number of arguments that can be passed to a function. Python has *args which allow us to pass the variable number of non keyword arguments to function.

In the function, we should use an asterisk * before the parameter name to pass variable length arguments.The arguments are passed as a tuple and these passed arguments make tuple inside the function with same name as the parameter excluding asterisk *.

Example 2: Using *args to pass the variable length arguments to the function

def adder(*num):
    sum = 0
    
    for n in num:
        sum = sum + n

    print("Sum:",sum)

adder(3,5)
adder(4,5,6,7)
adder(1,2,3,5,6)

When we run the above program, the output will be

Sum: 8
Sum: 22
Sum: 17