Python function default Argument Values

A python function can be defined in many ways. One of the ways allow us to specify a default value of the argument when the function is defined. This is known as Python function default Argument Values. The syntax of specifying the default argument value is shown below.

Link :Python function

>>> def func( arg1 , arg2=78 , arg3='Size')
... #your code here

The arg2 default value is 78 and the default value of arg3 is ‘size’.

The default argument value mentioned during the function definition is used as the argument’s value, if any value is not passed for that argument during the function call. Consider the code below.

  
>>> def func( arg1 , arg2=78 , arg3='Size' ):
	print('arg1=',arg1)
        print('\narg2=',arg2)
        print('\narg3=',arg3 )

	
>>> func( "New call!" )
arg1= New call! 
arg2= 78 
arg3= Size

We have not passed any values for arg1 and arg2 arguments so the default value is used.

Note: With default argument values we can call the function with lesser argument.


Overwriting the default argument values

Since the default argument values is written, it doesn’t mean we cannot pass a new value for that argument, we can. If we pass a new value then the default value is not used instead the value which is passed is used.

>>> def func( arg1 , arg2=78 , arg3='Size' ):
	print('arg1=',arg1)
        print('\narg2=',arg2)
        print('\narg3=',arg3 )

	
>>> func( 23 , 980 )
arg1= 23 
arg2= 980 
arg3= Size
>>> func( 'Happy call' , 1000 , 'Size of' )
arg1= Happy call 
arg2= 1000 
arg3= Size of

In the first call the arg2 default value 78 is overwritten with 980. In the second call the the arg2 and arg3 default values are overwritten with 1000 and ‘size of’.



Note:: Defining the default value does not constraint the type of the argument to the default value type.This means even if we have assigned a default value of integer to the argument we can still pass string or any other type to that argument. There is no rule stating that the argument will accept only integer type since the default value is an integer type.

>>> def func( arg1 , arg2=78 , arg3='Size' ):
	print('arg1=',arg1)
        print('\narg2=',arg2)
        print('\narg3=',arg3 )

	

>>> func( 'True type' , 'int' ) #the second argument is string
arg1= True type 
arg2= int 
arg3= Size
>>> #Passing 2nd argument as list
>>> func( 190 , [90 , 78 , 'text'] )  #Works fine
arg1= 190 
arg2= [90, 78, 'text'] 
arg3= Size
>>> #Passing 2nd and 3rd argument as string and floating-point value 
>>> func( 456 , 'New' , 56.789 ) #works fine
arg1= 456 
arg2= New 
arg3= 56.789

The above program still works fine.


The default value is evaluated only once

When using default argument value you should keep in mind that the default value is evaluated only once. This means if we make a subsequent call to the function, the function will still hold the value which was evaluated in the previous call. This might lead to some unexpected output. Note, such consequence mainly occur in a mutable object such as a list, dictionary, or instances of most classes. Consider the code below.

>>> def func( new , dic={} ):
	dic.update( new )
	return dic

>>> func( {2:'two'} )
{2: 'two'}
>>> func( {3 :'Three'} )
{2: 'two', 3: 'Three'}
>>> func( {10:'ten'} )
{2: 'two', 3: 'Three', 10: 'ten'}

Each time we pass a value to ‘func’ it is added to the exiting dictionary ‘dic’. Hence, the values are accumullated in each call.

To avoid such situation or if you do not want the value to be shared between each subsequent call then you must write the default value as ‘None‘. Look at the code below.

>>> def func( new , dic=None ):
	if dic is None:
		dic={}
	dic.update( new )
	return dic

Now try calling the function with different values, the output is the way it should be.

>>> func({56:'Fifty-six'})
{56: 'Fifty-six'}
>>> func( {66:'Sixty-six'} )
{66: 'Sixty-six'}


Leave a Reply

Your email address will not be published. Required fields are marked *