Blog Post #88: Name-Dropping: Using Keyword Arguments for Clarity

In Post #87, we learned about positional arguments and saw their biggest weakness: they depend entirely on the correct order. If you have a function with several parameters, it’s easy to forget their sequence and introduce a bug, like in our example where we accidentally called describe_pet('Harry', 'hamster') and got a nonsensical result.

To solve this problem and make our function calls more readable, Python provides another way of passing arguments: keyword arguments. In this post, you’ll learn how to pass arguments by name, making your code clearer and more robust.

What are Keyword Arguments?

A keyword argument is an argument that is passed to a function by explicitly stating the parameter’s name, followed by an equals sign and the value.

The syntax is: function_name(parameter_name = value)

This approach has two major benefits:

  1. Clarity: Your code becomes self-documenting. When you read the function call, there is no ambiguity about which value belongs to which parameter.
  2. Order Flexibility: Because you are explicitly naming the parameters, the order in which you provide the keyword arguments does not matter.

Keyword Arguments in Action

Let’s revisit our describe_pet function from the last post.

def describe_pet(animal_type, pet_name):
    print(f"I have a {animal_type}.")
    print(f"Its name is {pet_name}.")

Instead of relying on position, we can now call the function by naming our parameters.

# Calling with keyword arguments (in order)
describe_pet(animal_type="hamster", pet_name="Harry")

print() # Add a space between outputs

# Calling with keyword arguments (out of order - this still works perfectly!)
describe_pet(pet_name="Harry", animal_type="hamster")

Both of these function calls produce the exact same, correct output:

I have a hamster.
Its name is Harry.

Even though the arguments were in a different order in the second call, the result is correct because we explicitly told Python that the value "Harry" belongs to the pet_name parameter and "hamster" belongs to the animal_type parameter. This eliminates the logical error we saw in the previous post.

Combining Positional and Keyword Arguments

You can mix positional and keyword arguments in a single function call, but you must follow one simple and strict rule:

Positional arguments must always come before keyword arguments.

Once you use your first keyword argument, all subsequent arguments in that call must also be keyword arguments.

A Valid Example

This is a valid call because the positional argument ("cat") comes first.

# This is valid: positional first, then keyword
describe_pet("cat", pet_name="Whiskers")

An Invalid Example

This call will crash your program with a SyntaxError.

# This will cause a SyntaxError!
# describe_pet(pet_name="Fido", "dog")

The error occurs because after Python sees the keyword argument pet_name="Fido", it doesn’t know what to do with the positional argument "dog". This rule exists to avoid that ambiguity.

What’s Next?

Keyword arguments are a fantastic feature for improving the clarity and robustness of your function calls. By explicitly naming which parameter you are passing a value to, you eliminate order-based errors and make your code easier for others (and your future self) to understand.

So far, all the parameters we’ve defined have been required—we must provide an argument for them in the function call. But what if we want to make some parameters optional? In Post #89, we will learn how to provide default values for parameters, making our functions even more flexible.

Author

Debjeet Bhowmik

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com

Leave a Comment