**Jump To Right Section**Show

We can use the special syntax *args and **kwargs within a function definition in order to pass any number of arguments to a function. In this article, we guide you on how to efficiently use args and kwargs in Python.

Functions are reusable a block of codes performing a specific task, they make the program more effective and modular. While defining a function in Python, we also specify the parameters it can accept which we later pass as arguments in the function call. However, there might be situations when you are not aware of the total number of arguments required or you suspect that later on, the function may need additional arguments.

**Using *args In Python**

In Python,
*args is used to pass an arbitrary number of arguments to a function. It’s worth mentioning that the single asterisk( *** **) is the most important element, the word arg is just a naming convention we can name it anything it will work as long as we have a ( * ) before it. This could be better understood by the following example, Let’s create a function which can add numbers,

1 2 3 4 5 6 7 |
def total_sum(x, y): sum = x + y print(sum) total_sum(3,4) |

This simple function adds the two number which we pass in the function call. But what if we want to add three numbers? Unfortunately passing three arguments in a function call will lead to an error.

1 2 3 4 5 6 7 |
def total_sum(x, y): sum = x + y print(sum) total_sum(3,4,2) |

Output: TypeError: total_sum() takes 2 positional arguments but 3 were given.

The *args parameter is helpful in tackling such programming issues. Using *args parameter gives the function ability to accepts any number of positional arguments.

1 2 3 4 5 6 7 8 9 10 11 12 |
def total_sum(*args): sum = 0 for num in args: sum += num print("Sum :", sum) total_sum(3, 4) total_sum(3, 6, 7, 8,2) total_sum(3, 7, 8,2) total_sum(3, 8, 9) |

Output:

1 2 3 4 5 6 |
Sum : 7 Sum : 26 Sum : 20 Sum : 20 |

Now we can pass as many arguments as we wish and the function will work for all of them, this makes our code more modular and flexible. As stated above the asterisk(*) is the most important syntax we can replace args with literally anything yet it will work.

1 2 3 4 5 6 7 8 9 10 11 12 |
def total_sum(*anything): sum = 0 for num in anything: sum += num print("Sum :", sum) total_sum(3, 4) total_sum(3, 6, 7, 8,2) total_sum(3, 7, 8,2) total_sum(3, 8, 9) |

Output:

1 2 3 4 5 6 |
Sum : 7 Sum : 26 Sum : 20 Sum : 20 |

**Using **kwargs In Python**

Keyword arguments allow passing arguments as parameter names. In order to design a function which takes an arbitrary number of keywords arguments **kwargs in used as a parameter. It’s worth noting that the double-asterisk (**) is the key element here kwargs is just a convention you can use any word.

1 2 3 4 5 6 |
def print_kwargs(**kwargs): print(kwargs) print_kwargs(a=1, b=2, c="Some Text") |

Output: {'a': 1, 'b': 2, 'c': 'Some Text'}

We received all the keywords and their values in a dictionary(Key: value) form and we can perform every dictionary operation on it. Note that dictionaries are unordered (On Python 3.5 and lower ) so your result may vary for a large dictionary. Let’s extend the function to perform some basic dictionary operations.

1 2 3 4 5 6 7 |
def print_kwargs(**kwargs): for key in kwargs: print("The key {} holds {} value".format(key, kwargs[key])) print_kwargs(a=1, b=2, c="Some Text") |

Output:

1 2 3 4 5 |
The key a holds 1 value The key b holds 2 value The key c holds Some Text value |

**Using args and kwargs on the same function**

Keywords arguments are making our functions more flexible. We can use both args and kwargs on the same function in python as follows:

1 2 3 4 5 6 7 |
def function( *args, **kwargs): print(args) print(kwargs) function(6, 7, 8, a=1, b=2, c="Some Text") |

Output:

1 2 3 4 5 |
(7, 8) {'a': 1, 'b': 2, 'c': 'Some Text'} |

## Leave a Comment