Keyword Arguments in Python

This tutorial will teach you about Python keyword arguments and how to use them to make function calls more visible.

Let’s begin with a basic method for calculating the net price from the selling price and discount:

def get_net_price(price, discount):
    return price * (1-discount)

The price and discount parameters are passed to the get net price() function.

The following example explains how to use the get net price() function to get the net price from a price of 100 and a 10% discount:

net_price = get_net_price(100, 0.1)
print(net_price)

Output

90.0

Each argument is passed as a positional argument in the get net price(100, 0.1) function call. In other words, the price argument comes first, followed by the discount argument.

However, there is a readability issue with the method get net price(100, 0.1). Because you can’t tell which input is price and which is discount just by glancing at the function call.

Furthermore, you must know the position of each argument when calling the get net price() function.

If you don’t, the net price will be calculated wrongly by the function. For instance:

net_price = get_net_price(0.1, 100)
print(net_price)

Output

-9.9

Python uses keyword parameters to improve readability.

The keyword argument syntax is shown below:

fn(parameter1=value1,parameter2=value2)

You don’t have to declare the arguments in the same order as the function if you use the keyword argument syntax.

As a result, you can invoke a function by swapping the arguments as follows:

fn(parameter2=value2,parameter1=value1)

The following example explains how to call the get net price() function using the keyword parameter syntax:

net_price = get_net_price(price=100, discount=0.1)

or

net_price = get_net_price(discount=0.1, price=100)

They both produce the same consequence.

It’s the names of the keyword arguments that matter, not their places, when using them.

It’s worth noting that you can use both positional and keyword arguments to invoke a function. For instance:

net_price = get_net_price(100, discount=0.1)

default parameters and keyword arguments in python

Assume you have the get net price() function, which computes the net price using the selling price, tax, and discount.

def get_net_price(price, tax=0.07, discount=0.05):
    return price * (1 + tax - discount)

The tax and discount options in the get net price() function have default values of 7% and 5%, respectively.

The following utilises the default values for tax and discount arguments when calling the get net price() function:

net_price = get_net_price(100)
print(net_price)

Output

102.0

Assume you wish to utilise the default value for the tax parameter but not the discount parameter. The call to the following method is incorrect.

net_price = get_net_price(100, 0.06)

…because Python will give a price of 100 and a tax of 0.1, not a discount.

To correct this, use keyword arguments:

net_price = get_net_price(price=100, discount=0.06)
print(net_price)

Output

101.0

You can also use a combination of positional and keyword arguments:

net_price = get_net_price(100, discount=0.06)
print(net_price)

Output

101.0