python *args and **kwargs variable

Python *args and **kwargs Variable

Python *args and **kwargs Variable

With the help of Python *args and **kwargs Variable, you can pass variable(multiple) number of  argument to a function.The single asterisk form (*args) is used to pass a non-keyworded, variable-length argument list, and the double asterisk form(**kwargs) is used to pass a keyworded, variable-length argument list.

Here *args and **kwargs are naming conventions, only  *  asterisk is important. You can also write *var and **myvar.

 


What is *args ?

*args is used to pass variable number of argument to the function in Python.

We will see the use of *args with the help of an example. Suppose you have created a function to multiply two numbers:

[pastacode lang=”python” manual=”def%20multiply(x%2C%20y)%3A%0A%C2%A0%20%C2%A0%20print(%22Multiplication%20of%20X%20and%20Y%20is%22%2C%20x*y)” message=”Python Function to Multiply two Numbers” highlight=”” provider=”manual”/]

As you can see in the above program the function accepts only two arguments for multiplication. But what if you have to multiply more numbers(or to pass variable number of arguments to the function).

So here *args comes into picture. We can pass variable number of arguments to the function using *args. Lets see the below example:

[pastacode lang=”python” manual=”def%20multiply(*args)%3A%0A%C2%A0%20%C2%A0%20mul%20%3D%200%0A%C2%A0%20%C2%A0%20for%20i%20in%20args%3A%0A%C2%A0%20%C2%A0%20%C2%A0%20%C2%A0%20mul%20*%3D%20i%0A%C2%A0%20%C2%A0%20print(%22Multiplication%20is%22%2C%20mul)” message=”Passing variable number of arguments using *args” highlight=”” provider=”manual”/]

Now you can pass any number of arguments to the function:

 >>> multiply(1, 2, 3)
 6
 >>> multiply(1, 2, 3, 4)
 24
 >>> multiply(2, 4, 6, 8, 10,12)
 5760
 >>> multiply()
 0

Note: Here *args is just a naming convention, you can use anything that is valid identifier. For eg. *myargs.


Python **kwargs Variable

**kwargs allows you to pass keyworded variable length of arguments to a function. **kwargs passes variable like this emp_detail(empName='Sam', EmpId=101, team='Apps').

See the example below:

[pastacode lang=”python” manual=”def%20emp_detail(**kwargs)%3A%0A%C2%A0%20%C2%A0%20for%20i%2C%20j%20in%20kwargs.items()%3A%0A%C2%A0%20%C2%A0%20%C2%A0%20%C2%A0%20print(i%2C%20j)%0A%0Aemp_detail(empName%3D’Sam’%2C%20empId%3D101%2C%20team%3D’apps’)” message=”passing variable arguments using **kwargs” highlight=”” provider=”manual”/]

Expected Output:

empName  Sam
EmpId  101
team  Apps

Using *args and **kwargs to call a function

Let’s see the use of *args and **kwargs to call a function. Consider the following function definition:

def test_function(arg1, arg2, arg3):
    print("arg1 value:", arg1)
    print("arg2 value:", arg2)
    print("arg3 value:", arg3)

Now call the above function using *args and **kwargs:

# Use of *args to call test_function.
>>> args = ("Hi", 32, 10)
>>> test_args_kwargs(*args)
arg1 value: Hi
arg2 value: 32
arg3 value: 10

# Use of **kwargs to call the test_function.
>>> kwargs = {"arg3": 32, "arg2": "Hi", "arg1": 10}
>>> test_args_kwargs(**kwargs)
arg1: 10
arg2: Hi
arg3: 32





		
		
	

Leave a Reply