Improve Code Readability With Keyword Arguments In Python

One of the simplest ways of making Python code more readable is by replacing positional arguments in function calls with named or keyword arguments. Let’s explore how we can do that in this post!

Let’s create a basic function that takes in two arguments, a person’s first name and last name and prints out the greeting Hello <first_name> <last_name>!

def say_hello(first_name, last_name):
   print(f"Hello, {first_name} {last_name}!")

Let’s call the function that we created by passing in the required arguments like so:

say_hello("John", "Green")

As expected, the program outputs,

Hello, John Green!

But if we pass the person’s last name as the first argument in our function call and the person’s first name as the second argument in our function call, we get a slightly different result.

say_hello("Green", "John")

Output:

Hello, Green John!

These arguments that we passed to our function say_hello are called positional arguments as their positions in the function call will determine the value of their relevant parameters.

If a function accepts multiple arguments, then the only way Python has of determining which argument should be mapped to a function parameter is by its position in the function call.

While this is a relatively easy function that we are working with since it accepts only two arguments, it becomes difficult to pass arguments in the correct order for a function that accepts many parameters. To avoid this confusion when dealing with multiple parameters we can make use of keyword or named arguments.

These arguments are very helpful since they allow us to easily determine which argument maps to which parameter.

Let’s see how we can call our say_hello function with keyword arguments.

say_hello(first_name="John", last_name="Green")

We can even call our function by passing the last name before the first name like so:

say_hello(last_name="Green", first_name="John")

Output (in both cases):

Hello, John Green!

This is the same output that we got when we called our function with positional arguments like this:

say_hello("John", "Green")

We don’t have to stick to one type of argument when calling our functions; we can pass both positional and keyword arguments to our function calls:

say_hello("John", last_name="Green")

and get the same output:

Hello, John Green!

But what happens if we call our function by passing the keyword argument first and the positional argument next?

say_hello(first_name="John", "Green")

We get the following output:

SyntaxError: positional argument follows keyword argument

This happens because if we pass both positional and keyword arguments as parameters to a function call, we must pass the positional argument first and then the keyword argument.

While there are many ways of passing arguments in our functional calls, by choosing keyword arguments in functions that accept multiple arguments, we improve code readability. This not only helps us when we come back to the same piece of code after a long time but also acts as documentation for other engineers who will work with the same code.


If you would like to read more posts about Python Basics, you can find them here.

Thank you for reading!

License

Copyright 2021-present Vasudha Jha.

Released under the Creative Commons Attribution-ShareAlike 4.0 International License.

Vasudha Jha
Vasudha Jha
MS in Computer Science Student

An engineer, artist and writer, all at the same time, I suppose.

Related