Python keyword arguments

The Python keyword arguments is similar to the Python default argument values. In the keyword arguments, we require mentioning of the arguments name explicitly when the function is called. The syntax of the keyword argument is.


Note we can use both the default argument values and the keyword arguments together in a function definition.

Look at the function defined below and the various calls made to the function.

>>> def func(val , int=90 , st="String"):
...  print("val=",val ,"int=",int , "st=",st)
>>> func( "Newer" , int=1000 ) #'int' name explicitly mentioned
val= Newer int= 1000 st= String
>>> func(45 , 156 , st='2019/12/3') #'st' name explicitly mentioned
val= 45 int= 156 st= 2019/12/3
>>> func(st='New type', val=19010 , int=3456) #st,val,int explicitly mentioned#
val= 19010 int= 3456 st= New type

In the first func() call(line 4), we passed a value for ‘val’ argument and the second argument is passed as a keyword argument ‘int=100‘.

In the second call(line 6), only the third argument is passed as the keyword argument, which is ‘st=’2019/12/3′‘.

In the third func() call (line 8), all the arguments are passed as the keyword arguments st=’New type’, val=19010 and int=3456. In this call, do you notice the order of the arguments are not in sync with the argument lists order in the function definition. In the definition, the order is ‘val , int , st‘. This is allowed when all the arguments are passed as keyword arguments.

Here is one more example utilizing the keyword argument.

>>> def car(color , engine , brand='Audi' , horsePower='605hp'):
...  print("Car color=",color)
...  print("Engine type=",engine)
...  print("Brand =",brand)
...  print("Horse power=",horsePower)
>>> car('Grey', '1-litre') #No keyword argument
Car color= Grey
Engine type= 1-litre
Brand = Audi
Horse power= 605hp
>>> car(color='Blue', engine='1.5ltr', horsePower="900hp") #ALl keyword
Car color= Blue
Engine type= 1.5ltr
Brand = Audi
Horse power= 900hp
>>> car('Green' , '2ltr' , 'ROlls Royce' , '700hp') #No keyword
Car color= Green
Engine type= 2ltr
Brand = ROlls Royce
Horse power= 700hp
>>>  car('Black' , '2.5ltr' , horsePower='750hp and 560 lb-ft') #One keyword
Car color= Black
Engine type= 2.5ltr
Brand = Audi
Horse power= 750hp and 560 lb-ft

Some rules to follow while using the keywords argument

i)Passing value to a parameter is necessary if the argument does not have a default value

If one or more arguments does not have default values, we have to pass values for those arguments.

>>> def foo(new , type='all'):
...  print("new=",new, "type=",type)
>>> foo() #Error! 'new' requires a value
>>> def foo1(Move='Right'):
...  print('Move=',move)
>>> foo1() #Work fine

ii)Always use keyword argument after non-keyword argument(or positional argument)

If the function has arguments with and without the default values, in calling the function, if normal argument(also known as positional argument) is passed to the function, it must always preceed the keyword arguments.

>>>  def foo(new , type='all'):
...  print("new=",new, "type=",type)
>>> foo( type='Broad', 'string') #Error! positional argument after keyword arg
  File "<stdin>", line 1
SyntaxError: positional argument follows keyword argument
>>> foo('string' , type='Broad') #ok keyword argument after positional arg
new= string type= Broad

iii)We must not duplicate argument names

While calling a function we cannot duplicate an argument.

>>> def foo(new , type='all'):
...  print("new=",new, "type=",type)
>>> foo( 'String' , new='Text' ) #Error! 'new' argument duplicated

iv)We must not used non-existent keyword argument

If an argument name does not exist in the argument list in the function definition, we cannot pass it as a keyword argument using the non-existent name.

>>> def foo(new , type='all'):
...  print("new=",new, "type=",type)
>>> foo(new='Old' , width='100') #Error! 'width' is non-existent argument

v)The order does not matter if all the arguments are passed as keyword argument

When calling a function if all the arguments passed are of keyword arguments type, then the order does not matter.

>>> def foo(new , type='all' , street='Block'):
...  print('new=',new , "Type=",type, "Street=",street)
>>> foo(type='New' , new='Hip') #Work fine
new= Hip type= New
>>> foo( new='Old' , street="A" , type='GOld' ) #Work fine
new= Old Type= GOld Street= A
>>> foo(street='B' , new='Silver') #Work fine
new= Silver Type= all Street= B

If ther are *(single asterisk) and **(double asterisk) paramter then * must preceeds the ** parameter

If there are any * or ** parameters in a function, then the ** parameter must always come after the *(single asterisk) parameter. The order is important.

>>> def func(st , *single , **double): #Work fine
...  print(st)
>>> def foo( **double , *single ): #Error!!

More about the * parameter and the ** parameter discussed here Python arbitrary argument lists.