We use two operators *
(for tuples) and **
(for dictionaries).
# A Python program to demonstrate need
# of packing and unpacking
# A sample function that takes 4 arguments
# and prints them.
def fun(a, b, c, d):
print(a, b, c, d)
# Driver Code
my_list = [1, 2, 3, 4]
# This doesn't work
fun(my_list)
Output :
TypeError: fun() takes exactly 4 arguments (1 given)
Applications and Important Points
- Used in socket programming to send a infinite no. of requests to server.
- Used in Django framework to send variable arguments to view functions.
- There are wrapper functions that require us to pass in variable argurents.
- Modification of arguments become easy but at the same time validation is not proper so they must be used with care.