DS160123 - Argument Passing in python

In programming, we use functions to perform same operation with different inputs. This inputs are called as Arguments.
Now, in python we can pass arguments in three different ways,

  1. The first way is using one to one relation.

for example,

def fun1(a,b,c,d):
    print(a,b,c,d)
fun1(1,2,3,4)

def fun2(a=1,b=2,c=3,d=4):
   print(a,b,c,d)
fun2() # fun2 needs no arguments as they are assigned values already

def fun3(a,b=2,c=3,d):
   print(a,b,c,d)
fun3(1,4) #arguments gets assigned to the variables a and d respectively

Here, The arguments are in 1:1 relation i.e. there is one variable for every argument.
a=1, b=2,c=3,d=4
This is known as standard way and is great when there are small number of arguments and we know exact no. of arguments

  1. using one to many or many to one relation.

smart people call it as packing and unpacking respectively :nerd_face: :nerd_face:
This is used when we have large no. of arguments or we don’t know exact no. of arguments.

How does this work???

# packing example 
# many to one relation i.e. for many arguments we have
# only one variable

def fun1(*args):
    for i in args:
        print(i)
def fun2(**kwargs):
    for i,j in kwargs:
        print(i,'->',j)

fun1(1,'me',2,'my girl',3,'marriage',4,'children')
fun2(groom='me', bride='not found yet', marriage='who knows when')

As we can see, we are passing multiple arguments and but there is only one variable. This is possible because The variable we are using is a container with large capacity(i.e. tuple, dictionary)
So, we are taking multiple things and packing it into one variable (like a box).
And ‘*’ before the variable differentiate it from normal variable.
(i.e. args can take only one value but *args,**args can take multiple values, multiple key-values respectively)
Now, * and **, also tells python to pack the arguments in the form of tuple and dictionary respectively.
for unpacking,

# unpacking example
# one to many i.e. for one argument we have many variables
args = ['me again','still no one','still single']

def fun(a,b,c):
  print('a:',a,' ,b:',b,' c:',c)

fun(*args)

Here we have one argument (which itself contains many values) but multiple variables for it.
So, we unpack that argument and assign a value to the variables.

Note: In the example, we have predefined no. of variables so no. of values should be of exact same number. otherwise we can take argument as it is and unpack it inside the function using loop to create indefinite number of variables

Now, lets see an example:

def fun1(*l,**d):
     # unpack and run some code
def fun2(*l,a,**d):
    # unpack and run some code
def fun3(a,*l,**d):
    # unpack and run some code
    

fun1('you','one beside you','someone ghostlike behind you',
you='looks like person',beside_you='also looks like person',
remaining_one='not sure')

fun2('you','one beside you','someone ghostlike behind you',
you='looks like person',beside_you='also looks like person',
remaining_one='not sure')

fun3('me','you','one beside you','someone ghostlike behind you',
you='looks like person',beside_you='also looks like person',
remaining_one='not sure')

Here, fun1 runs smoothly where python packs first three arguments into variable l in form of tuple and remaining into variable d in the form of dictionary.( The syntax for argument passing is different for tuple and dictionary, so python can pack arguments in respective form).

But for fun2 python throws error (Invalid syntax) saying there is no argument for variable a, it is because there is no difference between passing an argument to normal variable and to packing variable. so python doesn’t know to save one argument for variable a.

we can tackle this issue by assigning normal variables(a) before packing variables (l,d) like in fun3.
Here ‘a’ is assigned value ‘me’ and remaining values gets packed.

Thank you for hanging till the end :saluting_face: and Hope you understand.
fill free to continue relevant info/doubts/corrections in the comments.
Don’t forget to like :smiling_face: :wink:
.

1 Like

@kharshavardhan31

Nice explanation, keep the pace up.
Keep Learning more.