Packing and Unpacking Arguments in Python

Packing and Unpacking Arguments in Python

Packing and Unpacking Arguments in Python With Example

args in python,kwargs in python,args and kwargs in python


All of you have done a little bit programming in python up to this, So there is a probability that you have heared  this name ‘*args’ and ‘**kwargs’. In this article we will learn what they are exactly.

The ‘*’ and ‘**’ operators does different operations which are complementary to each other. Lets start with the ‘*’ operator and use them into method definition

 

def all_total(*args,**kwargs):

    print(args)

all_total(1,2,3,4,5)

# it creates a tuple 


Output :

(1, 2, 3, 4, 5)

In above piece of code the operation done is called ‘packing’. It packs all the arguments into one. One thing should be keep in mind that instead of ‘args’ we can write anything with ’*’ operator but according to convention 99% coders write ‘args’, also it seems in a very pythonic way.

Consider that we have a function which takes three arguments exactly and we have a list of size 5 with us that has all the arguments for the function. Now if we simply try to pass list to the function, the call doesn’t work and through an error.

  

# A sample function that takes 5 arguments

 

def funt(a, b, c, d, e):

    print(a, b, c, d, e)

  

# Driver Code

list = [1, 2, 3, 4, 5]

  

# This doesn't work

funt(list)

 

Output :


TypeError: funt() missing 4 required positional arguments: 'b', 'c', 'd', and 'e'

 

Now we will see the unpacking of this code

 

Unpacking :

 

We will use * to unpack the list so that all present elements can be passed as different parameters.

 

def fun(a, b, c, d, e):

    print(a, b, c, d, e)

 

# Driver Code

lst = [1, 2, 3, 4, 5]

 

# Unpacking list into four arguments

fun(*lst)

 

Output :


1 2 3 4 5

 

Packing


There is a situation will come when we don’t know how many arguments rquired to be passed to a python function, we can use Packing to pack all arguments in a tuple.

 

def all_total(*args):

    total = 0

    for num in args :

        total += num

    return total

print(all_total(1, 2, 3))

 

Output :

6


*args with normal parameter

def multiply_nums(*args):

    multiply = 1

    for i in args :

        multiply *= i

    return multiply 

print(multiply_nums(1, 2, 3))

 

Output :

6

Lets see some more examples for better understanding

def my_sum(*args):

    sum = 0

    for i in range(0, len(args)):

        sum = sum + args[i]

    return sum

 

# Driver code

print(my_sum(1, 2, 3, 4, 5))

print(my_sum(10, 20))     

 

Output :

15

30


 

The ‘**’ operator is use for Dictionaries

# Program to demonstrate unpacking of dictionary items using **

 

def cod(a,b,c):

 

print("first item of dict: ",a)

print("\nsecond item of dict: ",b)

print("\nthird item of dict: ",c)

 

d = {'a': 1, 'b': 4, 'c': 8}

 

cod(**d)

 

Output :

first item of dict:  1

 

second item of dict:  4

 

third item of dict:  8


 

Lets see one more example

 

def cod(**kwargs):

# kwargs is a dict

print(type(kwargs))

# Printing dictionary items

for key in kwargs:

  print("%s = %s" % (key, kwargs[key]))

# Driver code

cod(name="dudle", page="91", language="Python")

 

Output :

<class 'dict'>

name = dudle

page = 91

language = Python


 In this article i have explained about packing and unpacking in python

Next Article : Type Conversion in Python

Recommended Post : Variable, Expression, Condition and Functions



                                       

                                       

 




#global variable python
#python variable scope
#python global
#python global variable#python programming
#python list
#learn python
#python requests
#introduction to data science in python
#free python tutorial
#learn python basics
#python study
#python and ai

Post a Comment

1 Comments

If you have any doubt, Please let me know.