Blog Post #87: Order Matters: How to Use Positional Arguments

In Post #86, we learned the crucial difference between parameters (the placeholders in a function definition) and arguments (the actual values we pass in a function call). Now, we need to understand how Python knows which argument to assign to which parameter.

The simplest and most common way Python does this is by position. In this post, we’ll explore positional arguments, where the order in which you pass your arguments is everything.

What are Positional Arguments?

Positional arguments are arguments that are matched to parameters based on their order, or position. The first argument in the function call is automatically assigned to the first parameter in the function definition, the second argument is assigned to the second parameter, and so on.

This is the default and most intuitive way of passing arguments in Python.

A Practical Example

Let’s create a function that takes two pieces of information: the type of an animal and its name. The function definition will have two parameters.

# The parameters are 'animal_type' and 'pet_name'
def describe_pet(animal_type, pet_name):
    print(f"I have a {animal_type}.")
    print(f"Its name is {pet_name}.")

Now, let’s call this function using positional arguments.

# The first argument, "hamster", maps to the first parameter, 'animal_type'.
# The second argument, "Harry", maps to the second parameter, 'pet_name'.
describe_pet("hamster", "Harry")

The output is exactly what we expect:

I have a hamster.
Its name is Harry.

The Danger of Getting the Order Wrong

With positional arguments, the name says it all: the position is critical. If you pass the arguments in the wrong order, your function will likely still run, but it can produce incorrect and nonsensical results.

# Calling the function with the arguments in the wrong order
describe_pet("Harry", "hamster")

Python has no way of knowing that “Harry” is a name and “hamster” is a type of animal. It just saw that “Harry” was the first argument, so it assigned it to the first parameter, animal_type. The result is illogical:

I have a Harry.
Its name is hamster.

This is a common source of bugs, especially in functions with many parameters.

Mismatched Number of Arguments

Python is strict about the number of positional arguments you provide. The number of arguments in your function call must exactly match the number of parameters in the function definition.

Too Few Arguments

If you forget to provide an argument, your program will crash with an error.

# This will cause a TypeError
# describe_pet("cat")

Running this code results in a TypeError: describe_pet() missing 1 required positional argument: 'pet_name'. Python helpfully tells you exactly what’s missing.

Too Many Arguments

Similarly, if you provide too many arguments, you’ll get a different TypeError.

# This will also cause a TypeError
# describe_pet("dog", "Fido", 5)

This results in TypeError: describe_pet() takes 2 positional arguments but 3 were given.

What’s Next?

Positional arguments are the straightforward, default way of passing data to functions. They are easy to use, but you must be careful to pass them in the correct order and provide the correct number of arguments.

Relying on position works well for functions with one or two parameters, but it can become confusing and error-prone with more. What if you can’t remember the exact order of the parameters? To solve this, Python provides a more explicit way of passing arguments. In Post #88, we will learn about keyword arguments.

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