In this guide, we’re going to talk about the “positional argument follows keyword argument” error and why it is raised. We’ll look at an example code snippet with this error so that we can walk through how to solve it.
Let’s get started!
The Problem: positional argument follows keyword argument
Let’s take a look at our full error:
SyntaxError: positional argument follows keyword argument
Like the English language, programming languages have their own rules. These rules are referred to as syntax. Our error is a syntax error which means that we have failed to follow one of the rules that governs how to write a Python code.
The next part of our error tells us what is causing this error. In this case, our code must have a positional argument that appears after a keyword argument.
Positional arguments are arguments that appear in their respective positions:
def add_numbers(a, b): return a + b Let's call this function: add_numbers(2, 3)
“a” and “b” become variables inside our function. This code works because we have specified two positional arguments. “a” is equal to 2 and “b” is equal to three. We can also specify these arguments as keyword arguments:
However, we cannot specify a positional argument first and then switch to the keyword syntax.
This is because Python has a special function called *args which processes multiple arguments in a function. Consider this code:
def show_users(a, b, *args): print(a, b, *args)
This code uses *args. This keyword represents a variable number of arguments. We can pass in as many arguments to our
show_users() function as we want:
show_users("Alex", "Peter", "Violet', "Julie")
Our code returns: Alex Peter Violet Julie
Our first two arguments, “a” and “b”, have the values “Alex” and “Peter”, respectively.
This is because if you use positional syntax, arguments are assigned in the order in which they are passed. The last arguments appear in the order that they are stated because *args represents an unknown amount of additional arguments.
An Example Scenario
Let’s take a look at a code snippet that experiences this error:
def print_menu(salads, pizzas): print("Salad Menu") for s in salads: print(s) print("") print("Pizza Menu") for p in pizzas: print(p)
This function accepts two arguments: salads and pizzas. Our function prints out each salad on the salad menu and each pizza on the pizza menu to the console.
Let’s call our function:
salads = ["Tuna Salad", "Lettuce and Mango Salad", "Greek Salad"] pizzas = ["Veggie Supreme", "Ham and Pineapple", "BBQ Chicken"] print_menu(pizzas=pizzas, salads)
Our code returns:
File "main.py", line 13 print_menu(pizzas=pizzas, salads) ^ SyntaxError: positional argument follows keyword argument
There’s an error in our code, as we expected. Let’s fix it.
To solve this problem, we need to make sure that all positional arguments come before keyword arguments. Let’s change how we call our function to reflect this rule:
We have specified two positional arguments: salads and pizzas. Alternatively, we could specify “pizzas” as as keyword argument after “salads”:
print_menu(salads, pizzas = pizzas)
In this example, it is unnecessary to add in any keyword arguments because we are not using the *args method. With that said, adding in keyword arguments can make code more readable depending on the number of values being passed into a function.
Let’s run our code with this revised function call:
Salad Menu Tuna Salad Lettuce and Mango Salad Greek Salad Pizza Menu Veggie Supreme Ham and Pineapple BBQ Chicken
Our code successfully prints out our two Python lists.
Positional arguments must appear before a keyword argument in Python. This is because Python interprets positional arguments in the order in which they appear. Then, it interprets the keyword arguments that have been specified.
Now you’re ready to solve the “positional argument follows keyword argument” error like an expert Python developer!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.