Python offers various data structures, collections, and ways to store, pass and use arguments while coding. It provides a dynamic feature to pack and unpack arguments when dealing with functions to improve the readability and adaptability of code.
Packing arguments: It refers to aggregating multiple arguments in a single collection so that the operations can be applied to the arguments combined. It is helpful when the same operation is to be called with a different number of parameters.
Unpacking arguments: It refers to deconstructing a collection into multiple arguments so that the operations can be applied to the arguments individually. It is helpful when the arguments are required separately to perform operations.
In this example, we send a dynamic number of arguments as parameters to the Add
function and access them through one tuple collection parameter i.e. myTuple
, by adding the *
operator before it. Note that the same Add
function is called with a different number of parameters in it.
def Add(*myTuple):print(type(myTuple))return sum(myTuple)print(Add(2, 4, 5, 6, 7, 8, 12))print(Add(20, 40, 60, 80))
In this example, we create a tuple and send it as a parameter to the Add
function by adding the *
operator before it. The Add
function receives it as individual parameters and deconstructs it to map each argument on a parameter. The separated arguments are then summed and printed.
It is important to have the same number of tuple arguments and function parameters to map them well. Note that myTuple
is unpacked and gives the correct output, but myTuple2
throw an error because the no. of arguments != no. of parameters.
def Add(num1 , num2 , num3):print(num1 + num2 + num3)#works wellmyTuple = (2, 4, 6)Add(*myTuple)#gives errormyTuple2 = (8 , 12)Add(*myTuple2)
In this example, we send a dynamic number of arguments as parameters to the printDetails
function and access them through one dictionary collection parameter i.e. myDict
by adding the **
operator before it. Note that the same printDetails
function is called with a different number and type of parameters in it. The function loops through the keys in the dictionary and prints the key and its corresponding value.
def printDetails(**myDict):print(type(myDict))# Printing dictionary itemsfor key in myDict:print((key, myDict[key]))printDetails(num1 = '2', num2 = '4', num3 = '6')print('\n')printDetails(fruit = 'mango', colour = 'purple')
In this example, we create a dictionary and send it as a parameter to the Add
function by adding the **
operator before it. The Add
function receives it as individual parameters and deconstructs it to map each argument on a parameter. The separated arguments are then summed and printed.
It is important to have the same number of arguments in the dictionary and function parameters to map them well. Note that myDict
is unpacked and gives the correct output, but myDict2
throw an error because the no. of arguments != no. of parameters.
def Add(num1 , num2 , num3):print(num1 + num2 + num3)#works wellmyDict = {'num1' : 12, 'num2' : 8, 'num3' : 2}Add(**myDict)#gives errormyDict2 = {'num1' : 12, 'num2' : 8}Add(**myDict2)
Improved code reusability: The same function can handle a wide range of inputs without explicitly defining all parameters.
Concise function calls: In case of more arguments, all the arguments can be sent under in parameter.
Improved code readability: Separately defining all the arguments explicitly makes it easier to understand the purpose of each value at a glance.
Simultaneous assignments: All the arguments can be separately assigned concurrently, improving time efficiency.
def showData(a, b, c):
print(a, b, c)
myData = (1, 2, 3)
showData(myData)
What will be the output for this?
(1, 2, 3)
Error because the *
operator is missing before the myData
parameter.
Error because the **
operator is missing before the myData
parameter.
Free Resources