Python Data Persistence – Keyword Arguments

Python Data Persistence – Keyword Arguments

We now know that the values passed to a function are read as per the order in which parameters are defined. However, if you do want to pass values out of order, use the parameter’s name as keyword. Following code snippet will illustrate the point:

Example

#func-with-keywords.py
def division(num, denom):
div=num/denom
print ('numerator: { } denominator: { } division: { } ' . format ( num , denom , div ) )
division ( 10 , 2 )
division ( 2 , 10 )
division(denom=2, num=10)

In the first two cases, the first argument becomes numerator, and the second becomes denominator as values are picked up by formal parameter names as per the order. In the last case, the name of the parameter is used as a keyword so that even if values are passed out of order, they go in desired parameters of the function.

Output:

E:\python3 7>python func-with-keywords.py 
numerator:10 denominator: 2 division: 5.0 
numerator:2 denominator: 10 division: 0.2 
numerator:10 denominator: 2 division: 5.0

Note that in the above example, the use of keyword arguments is optional. To force compulsory use of keyword arguments, use as one parameter in the parameter list. All parameters after * become keyword-only parameters. Any parameters before * continue to be positional parameters.

The above example is modified so that both parameters are made keyword-only by putting * as the first parameter in the list. Now if you try to call the division( ) function without specifying keywords, TypeError is raised.

Example

#func-with-kwonly.py
def division( * , num , denom ) :
div=num/denom
print ('numerator:{ } denominator: { } division: { } ' . format(num , denom , div ) )
division(denom=2, num=10)
division(10,2)

Output

E:\Python37>Python func-with-kwonly.py
numerator:10 denominator: 2 division: 5.0 
Traceback (most recent call last): 
File "func-with-kwonly.py", line 7, in <module> division(10,2) 
TypeError: division( ) takes 0 positional arguments but 2 were given

Python uses the mechanism of keyword arguments and arguments with default value very extensively in defining built-in functions and methods. One such example is int ( ) function. In fact, we had discussed int() function in chapter 1. It can be used as per two prototype definitions:

int([x])
 int(x, base=10)

In the first definition, the parameter x should be a number. The function returns its integer part. In the second case, x must be a string containing digits only. The second parameter – base has a default value of 10. But it can be 2, 8, 16, and so on. i.e. base of binary, octal, or hexadecimal number system. The string is then converted to a number accordingly. The base parameter can be used like a normal positional argument or keyword argument.

Example

>>> #converts float to int
>>> int ( 123 . 45 )
123
>>> #converts string to number with default base 10 - decimal number system
. . .
>>> int('121')
121
>>> #converts string with hexadecimal digits - base=16 as positional argument
. . .
>>> int('121',16)
289
>>> #converts string with hexadecimal digits - base=16 as keyword argument
. . .
>>> int('121',base=16)
289