11.11. Function Defination#

  • Positional arguments

    • by default ones

      • def foo(a,b)

      • foo(2,3) # Here a and b are positional arguments and you need to pass both..If u miss even one,error will come

    • means,the order in which we pass values when we call the function gets assigned its arguments respectively

  • Keyword arguments

    • you specify it explicitly using = symbol

      • def foo(a,b=’sahil’)

      • foo(2,3) # Here a is positional but b is keyword argument so is optional and you need to pass both..If u miss even one,error will come

  • Note: Keyword arguments should always be in the last during function declaration

11.11.1. Make any argument as optional#

def hi_random_person(name,age=None):
    print('Hi',name)
    if age:
        print('I am '+ str(age)+' years old')
hi_random_person('sahil')
Hi sahil
hi_random_person('sourav',24)
Hi sourav
I am 24 years old

11.11.2. Set a default value for a argument#

def hi(name,place='Jammu'):   # note: here name is called positional argument and place is called keyword argument
    print('Hi ' + name+ " from "+ place)
hi('sahil')
Hi sahil from Jammu
hi('sourav','Mumbai')
Hi sourav from Mumbai

11.11.2.1. Use case#

# Problem
def add(a,b):
    return a+b

print(add(1,2))
#print(add(1))  # it will throw error

# Solution
def add(a,b=0): # or b=None
    return a+b

print(add(1,2))
print(add(1))  # it will throw error
3
3
1

11.11.3. When you are unsure about the number of arguments - For positional arguments#

  • Use single asterick

  • *args

  • Note: it picks all the arguments after the positional arguments are assigned

  • args is tupple by default

def hi(*arguments): 
    return 'hi'+ str(arguments)
print(hi('sahil'))
print(hi('sahil','sourav'))
hi('sahil',)
hi('sahil', 'sourav')

11.11.4. When you are unsure about the number of arguments and you don’t want to assign arguments in advance - For keyword arguments#

  • Use double asterick

  • **kwargs

  • **kwargs is dictinary by default

  • Note: kwargs (or **) only picks up the = arguments

  • common usecase is when u wrote one function for superclass and in subclasses you need only few arguments

def hi(**arguments):
    return 'hi'+ str(arguments)
print(hi('sahil')) # it will give error :hi() takes 0 positional arguments but 1 was given because
# kwargs (or **) only picks the values passed as =
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [11], in <cell line: 1>()
----> 1 print(hi('sahil'))

TypeError: hi() takes 0 positional arguments but 1 was given
print(hi(name='sahil'))
hi{'name': 'sahil'}
print(hi(name1='sahil',name2='sourav'))
hi{'name1': 'sahil', 'name2': 'sourav'}

11.11.5. Summary (All used in one Example)#

def hi(a,b,*args,**kwargs):
    return 'hi '+str(a)+ str(b)+ str(args)+ str(kwargs)
hi(10,20) # both these will be picked by positional arguments (a,b) only 
# also,if you even miss one value and pass only one value,it will throw error.
# all the positional arguments must be passed always.This is the rule
'hi 1020(){}'
hi(10,20,30,40) # after taking and b from arguments,rest everything will be taken as args
# and since args comes as tupple by default,they will be in tupple
'hi 1020(30, 40){}'
hi(10,20,30,40,50,60) # after taking and b from arguments,rest everything will be taken as args
# and since args comes as tupple by default,they will be in tupple
# Note: No kwargs will be taken because nothing is passed as =
'hi 1020(30, 40, 50, 60){}'
hi(10,20,30,40,50,60,x=70,y=80) # x and y will be taken as kwars since they value =
# and kwargs comes as dictionary by default
"hi 1020(30, 40, 50, 60){'x': 70, 'y': 80}"