{"id":1106,"date":"2024-10-17T19:33:44","date_gmt":"2024-10-17T14:03:44","guid":{"rendered":"https:\/\/www.mrcoder701.com\/?p=1106"},"modified":"2024-10-17T19:33:47","modified_gmt":"2024-10-17T14:03:47","slug":"args-kwargs-python","status":"publish","type":"post","link":"https:\/\/www.mrcoder701.com\/2024\/10\/17\/args-kwargs-python\/","title":{"rendered":"How To Use *args and **kwargs In Python"},"content":{"rendered":"

Functions are reusable blocks of code that perform a specific task; they make the program more efficient and modular.<\/p>

When we define a function in Python, we also specify the parameters it can accept, which we then pass as arguments in the function call.<\/p>

However, there may be instances where you are unaware of the total number of arguments required or suspect that the function will require additional arguments in the future.<\/p>

To pass any number of arguments to a function, use the special syntax *args<\/strong> and **kwargs<\/strong> within the function definition. In this article, we will show you how to use *args<\/strong> and **kwars<\/strong> in Python efficiently.<\/p>

\n <\/path>\n<\/svg><\/div><\/div>

<\/p>

*args In Python<\/h1>

In Python, the *args keyword is used to pass an arbitrary number of arguments to a function. It’s worth noting that the single asterisk (*) is the most important element; the word arg is simply a naming convention; we can call it whatever we want as long as we put a (*) before it.<\/p>

This could be better understood by the following example, Let’s create a function which can add numbers,<\/p>

<\/circle><\/circle><\/circle><\/g><\/svg><\/span><\/path><\/path><\/svg><\/span>
def<\/span> <\/span>total_sum<\/span>(<\/span>x<\/span>, <\/span>y<\/span>):<\/span><\/span>\n    <\/span>sum<\/span> <\/span>=<\/span> x <\/span>+<\/span> y<\/span><\/span>\n    <\/span>print<\/span>(<\/span>sum<\/span>)<\/span><\/span>\n<\/span>\ntotal_sum(<\/span>6<\/span>,<\/span>7<\/span>)<\/span><\/span><\/code><\/pre><\/div>

This simple function adds the two number which we pass in the function call. But what if we want to add three numbers?<\/p>

Unfortunately passing three arguments in function call will lead to an error.<\/p>

<\/circle><\/circle><\/circle><\/g><\/svg><\/span><\/path><\/path><\/svg><\/span>
def<\/span> <\/span>total_sum<\/span>(<\/span>x<\/span>, <\/span>y<\/span>):<\/span><\/span>\n    <\/span>sum<\/span> <\/span>=<\/span> x <\/span>+<\/span> y<\/span><\/span>\n    <\/span>print<\/span>(<\/span>sum<\/span>)<\/span><\/span>\n<\/span>\ntotal_sum(<\/span>7<\/span>,<\/span>2<\/span>,<\/span>1<\/span>)<\/span><\/span>\n<\/span>\n#output<\/span><\/span>\nTypeError<\/span>: total_sum() takes <\/span>2<\/span> positional arguments but <\/span>3<\/span> were given<\/span><\/span>\n<\/span><\/code><\/pre><\/div>

The\u00a0*args<\/code>\u00a0parameter is helpful in tackling such programming issues. Using\u00a0\u00a0*args<\/code>\u00a0parameter gives the function ability to accepts any number of\u00a0positional arguments<\/strong>.<\/p>

<\/circle><\/circle><\/circle><\/g><\/svg><\/span><\/path><\/path><\/svg><\/span>
def<\/span> <\/span>total_sum<\/span>(<\/span>*<\/span>args<\/span>):<\/span><\/span>\n    <\/span>sum<\/span> <\/span>=<\/span> <\/span>0<\/span><\/span>\n    <\/span>for<\/span> num <\/span>in<\/span> args:<\/span><\/span>\n        <\/span>sum<\/span> <\/span>+=<\/span> num<\/span><\/span>\n    <\/span>print<\/span>(<\/span>"<\/span>Sum :<\/span>"<\/span>, <\/span>sum<\/span>)<\/span><\/span>\n<\/span>\ntotal_sum(<\/span>4<\/span>, <\/span>8<\/span>)<\/span><\/span>\ntotal_sum(<\/span>2<\/span>, <\/span>5<\/span>, <\/span>6<\/span>, <\/span>4<\/span>,<\/span>4<\/span>)<\/span><\/span>\ntotal_sum(<\/span>9<\/span>, <\/span>8<\/span>, <\/span>4<\/span>,<\/span>1<\/span>)<\/span><\/span>\ntotal_sum(<\/span>6<\/span>, <\/span>3<\/span>, <\/span>5<\/span>)<\/span><\/span>\n<\/span>\n#output<\/span><\/span>\nSum : <\/span>12<\/span><\/span>\nSum : <\/span>21<\/span><\/span>\nSum : <\/span>22<\/span><\/span>\nSum : <\/span>14<\/span><\/span><\/code><\/pre><\/div>

Now we can pass as many arguments we wish and the function will work for all of them, this makes our code more modular and flexible.<\/p>

As stated above the asterisk(*) is the most important syntax we can replace args with literally anything yet it will work.<\/p>

<\/circle><\/circle><\/circle><\/g><\/svg><\/span><\/path><\/path><\/svg><\/span>
def<\/span> <\/span>total_sum<\/span>(<\/span>*<\/span>anything<\/span>):<\/span><\/span>\n    <\/span>sum<\/span> <\/span>=<\/span> <\/span>0<\/span><\/span>\n    <\/span>for<\/span> num <\/span>in<\/span> anything:<\/span><\/span>\n        <\/span>sum<\/span> <\/span>+=<\/span> num<\/span><\/span>\n    <\/span>print<\/span>(<\/span>"<\/span>Sum :<\/span>"<\/span>, <\/span>sum<\/span>)<\/span><\/span>\n<\/span>\ntotal_sum(<\/span>4<\/span>, <\/span>8<\/span>)<\/span><\/span>\ntotal_sum(<\/span>2<\/span>, <\/span>5<\/span>, <\/span>6<\/span>, <\/span>4<\/span>,<\/span>4<\/span>)<\/span><\/span>\ntotal_sum(<\/span>9<\/span>, <\/span>8<\/span>, <\/span>4<\/span>,<\/span>1<\/span>)<\/span><\/span>\ntotal_sum(<\/span>6<\/span>, <\/span>3<\/span>, <\/span>5<\/span>)<\/span><\/span>\n<\/span>\n#output<\/span><\/span>\nSum : <\/span>12<\/span><\/span>\nSum : <\/span>21<\/span><\/span>\nSum : <\/span>22<\/span><\/span>\nSum : <\/span>14<\/span><\/span><\/code><\/pre><\/div>

<\/p>

\n <\/path>\n<\/svg><\/div><\/div>

<\/p>

Using **kwargs\u00a0In Python<\/h1>

Keyword arguments allow arguments to be passed as parameter names. To design a function that accepts an arbitrary number of keyword arguments, kwargs<\/strong> is used as a parameter. It is worth noting that the double astrick (**) is the key element here, kwargs is simply a convention; any word can be used.<\/p>

<\/circle><\/circle><\/circle><\/g><\/svg><\/span><\/path><\/path><\/svg><\/span>
def<\/span> <\/span>print_kwargs<\/span>(<\/span>**<\/span>kwargs<\/span>):<\/span><\/span>\n    <\/span>print<\/span>(kwargs)<\/span><\/span>\n<\/span>\nprint_kwargs(<\/span>a<\/span>=<\/span>4<\/span>, <\/span>b<\/span>=<\/span>3<\/span>, <\/span>c<\/span>=<\/span>"<\/span>MrCoder701<\/span>"<\/span>)<\/span><\/span>\n<\/span>\n#output<\/span><\/span>\n{<\/span>'<\/span>a<\/span>'<\/span>: <\/span>4<\/span>, <\/span>'<\/span>b<\/span>'<\/span>: <\/span>3<\/span>, <\/span>'<\/span>c<\/span>'<\/span>: <\/span>'<\/span>MrCoder701<\/span>'<\/span>}<\/span><\/span><\/code><\/pre><\/div>

We received all of the keywords and their values in the form of a dictionary (Key: value), which allows us to perform all dictionary operations on it. Note that dictionaries are unordered (on Python 3.5 and lower), so your results may differ for a large dictionary.<\/p>

Let’s extend the function to perform some basic dictionary operations.<\/p>

<\/circle><\/circle><\/circle><\/g><\/svg><\/span><\/path><\/path><\/svg><\/span>
def<\/span> <\/span>print_kwargs<\/span>(<\/span>**<\/span>kwargs<\/span>):<\/span><\/span>\n    <\/span>for<\/span> key <\/span>in<\/span> kwargs:<\/span><\/span>\n        <\/span>print<\/span>(<\/span>"<\/span>The key <\/span>{}<\/span> holds <\/span>{}<\/span> value<\/span>"<\/span>.format(key, kwargs[key]))<\/span><\/span>\n<\/span>\nprint_kwargs(<\/span>a<\/span>=<\/span>4<\/span>, <\/span>b<\/span>=<\/span>3<\/span>, <\/span>c<\/span>=<\/span>"<\/span>MrCoder701<\/span>"<\/span>)<\/span><\/span>\n<\/span>\n#output<\/span><\/span>\nThe key a holds <\/span>4<\/span> value<\/span><\/span>\nThe key b holds <\/span>3<\/span> value<\/span><\/span>\nThe key c holds MrCoder701 value<\/span><\/span><\/code><\/pre><\/div>

Keywords arguments are making our functions more flexible.<\/p>

In Python, we can use both *args<\/code> and **kwargs<\/code> on the same function as follows:<\/p>

<\/circle><\/circle><\/circle><\/g><\/svg><\/span><\/path><\/path><\/svg><\/span>
def<\/span> <\/span>function<\/span>( <\/span>*<\/span>args<\/span>, <\/span>**<\/span>kwargs<\/span>):<\/span><\/span>\n    <\/span>print<\/span>(args)<\/span><\/span>\n    <\/span>print<\/span>(kwargs)<\/span><\/span>\n<\/span>\nfunction(<\/span>2<\/span>, <\/span>5<\/span>, <\/span>4<\/span>, <\/span>a<\/span>=<\/span>7<\/span>, <\/span>b<\/span>=<\/span>5<\/span>, <\/span>c<\/span>=<\/span>"<\/span>MrCoder701<\/span>"<\/span>)<\/span><\/span>\n<\/span>\n#output<\/span><\/span>\n(<\/span>2<\/span>, <\/span>5<\/span>, <\/span>4<\/span>)<\/span><\/span>\n{<\/span>'<\/span>a<\/span>'<\/span>: <\/span>7<\/span>, <\/span>'<\/span>b<\/span>'<\/span>: <\/span>5<\/span>, <\/span>'<\/span>c<\/span>'<\/span>: <\/span>'<\/span>MrCoder701<\/span>'<\/span>}<\/span><\/span><\/code><\/pre><\/div>

<\/p>

\n <\/path>\n<\/svg><\/div><\/div>

<\/p>

Difference between *args<\/code> and **kwargs<\/code> in Python?<\/h1>
Aspect<\/strong><\/td>*args<\/strong><\/td>**kwargs<\/strong><\/td><\/tr>
What it is?<\/strong><\/td>A way to pass multiple<\/em> positional arguments.<\/td>A way to pass multiple<\/em> keyword arguments.<\/td><\/tr>
What it looks like<\/strong><\/td>Like this: func(1, 2, 3)<\/code>. You pass values by position.<\/td>Like this: func(a=1, b=2, c=3)<\/code>. You pass values by name.<\/td><\/tr>
How to access<\/strong><\/td>Inside the function, it’s a tuple<\/em> of values.<\/td>Inside the function, it’s a dictionary<\/em> of values.<\/td><\/tr>
When to use it<\/strong><\/td>Use when the number of arguments isn\u2019t fixed. Example? Maybe you want to add up numbers: add(1, 2, 3)<\/code>.<\/td>Use when you need named arguments. Think: options<\/strong> or settings<\/strong>. Example? create_user(name=\"John\", age=30)<\/code>.<\/td><\/tr>
Common scenario<\/strong><\/td>Passing a bunch of numbers or strings. Like a list but easier.<\/td>Named parameters. Think forms or filters. Like search(query=\"shoes\", limit=10)<\/code>.<\/td><\/tr>
Fun fact<\/strong><\/td>You can combine it with regular arguments. Neat, right?<\/td>Same here. You can mix *args and **kwargs in one function.<\/td><\/tr>
Quick takeaway<\/strong><\/td>Positional<\/em> arguments packed as a tuple. Use when you don\u2019t care about names.<\/td>Keyword<\/em> arguments packed as a dictionary. Use when names matter.<\/td><\/tr><\/tbody><\/table>
Difference between\u00a0*args<\/code>\u00a0and\u00a0**kwargs<\/code>\u00a0in Python?
<\/figcaption><\/figure>
\n <\/path>\n<\/svg><\/div><\/div>

<\/p>

*args and **kwargs in Python \u2013 FAQs<\/h1>

<\/p>

Why use\u00a0*args<\/code>\u00a0and\u00a0**kwargs<\/code>\u00a0in Python?<\/h6>

*args<\/strong><\/code><\/strong> and *<\/code>*kwargs<\/strong><\/code><\/strong> allow functions to accept a variable number of arguments:<\/em><\/p>