When working with Python functions, you might encounter an error as follows:
SyntaxError: positional argument follows keyword argument
This error usually occurs when you call a function that has both positional and named parameters.
In Python 2, the error message is slightly reworded as:
SyntaxError: non-keyword arg after keyword arg
Both error messages give us hints on how to fix this error. This article explains the error in more detail and shows you how to fix it.
How this error happens
This error happens when you call a function and place an unnamed argument next to a named argument.
Let’s see an example that causes this error. Write a function named greet()
with the following parameters:
def greet(name, age, hobby):
print(f"Name: {name}")
print(f"Age: {age}")
print(f"Hobby: {hobby}")
Next, call the function and pass the arguments as shown below:
greet("Nathan", age=28, "Coding")
Output:
Traceback (most recent call last):
File "main.py", line 6
greet("Nathan", age=28, "Coding")
^
SyntaxError: positional argument follows keyword argument
The error complains that we placed a positional argument next to the keyword argument.
A keyword argument (also known as named argument) is a value that you pass into a function with an identifying keyword or name.
By contrast, a positional argument is an unnamed argument, and is identified by its position in the function call.
By default, Python arguments are positional arguments, and they must not be specified after keyword arguments when you call a function.
How to fix this error
To resolve this error, you need to make sure that you don’t pass any positional arguments after keyword arguments.
You need to pass another keyword argument after the keyword argument age=
as follows:
def greet(name, age, hobby):
print(f"Name: {name}")
print(f"Age: {age}")
print(f"Hobby: {hobby}")
greet("Nathan", age=28, hobby="Coding") # ✅
You can use keyword arguments to pass arguments in a different order than the defined parameters.
All following examples could work:
greet("Nathan", hobby="Coding", age=28)
greet(hobby="Coding", age=28, name="Nathan")
greet(age=28, name="Nathan", hobby="Coding")
Keyword arguments are optional, so you can also pass the same arguments without giving them names.
But you need to pass them in the same order as the parameters defined in the function:
greet("Nathan", 28, "Coding")
Another thing that can cause this error is when you mistyped the keyword argument using the comparison ==
operator.
Pay close attention to the hobby
argument below:
greet(name="Nathan", age=28, hobby=="Coding")
# Traceback (most recent call last):
# File "main.py", line 7
# greet(name="Nathan", age=28, hobby=="Coding")
# ^
# SyntaxError: positional argument follows keyword argument
In this example, we intend to pass a keyword argument named hobby
, but the comparison operator turns the named argument into an expression that returns either True
or False
.
When passing keyword arguments, make sure that you use the assignment =
operator to avoid this error.
I hope this tutorial is helpful. Happy coding! 👍