How to Fix the Python Error: SyntaxError: positional argument follows keyword argument

Python is a language with a lot of flexibility and a distinct lack of firm restrictions. The language’s interpreter does, of course, impose some syntactical limitations. But for the most part, you’re free to form your code around personal preferences. There are some surprising caveats to that rule though. And some, like the “SyntaxError: positional argument follows keyword argument” Python error, tend to catch people by surprise. But while the error is surprising, it’s quite straightforward to fix when you know what’s actually going on.

What Does the SyntaxError Mean?

This issue is particularly tricky because the wording of the error message can be somewhat confusing. To understand the error we need to start with the positional argument. A positional argument describes situations where an element needs to be passed in a fixed position for a particular function to operate correctly. The next part of the error message points out the incorrect use of a keyword argument.

A keyword, in this context, means an argument that has a label or assignment. Python’s syntax requires us to use any positional arguments before we can use a keyword argument. The error message is complaining that a line of code isn’t following that rule. Essentially, the error means that we’re trying to do the Python version of the old expression “putting the cart before the horse”. A cart can’t pull a horse and a keyword argument can’t come before a positional.

A Closer Look at Python’s Syntax

As with many syntax errors, this issue is a lot easier to understand with an example. Try running the following code.

def ourFunc(x, y, z):
    ourList = [1,2,3,4,5,6]
    print(“x=” + str(x) + ” y=” + str(y) + ” z=” + str(z) + ” ourList=” + str(ourList[z]))

ourFunc(1, 2, 3)

We begin by defining a function that takes three parameters. It expects numerical values for an x,y, and z variable. On line 2 we create a list called ourList and populate it with the numbers 1 to 6. Though note that we’re counting from 1 rather than 0. This numbering scheme becomes important in the next line.

On line 3 we actually make use of our variables. We print out the variables by passing them to str and concatenating them with some descriptor text. We also use the z variable as a position indicator for ourList. Finally, on line 5, we actually call the ourFunc function by passing three numbers to it. In this and further permutations on this example, note that the z variable is being successfully used to point to a position in ourList. This is both to show that the variables are functioning as expected and that “position” is used in a very specific context within the syntax error. A positional error doesn’t always indicate an error in position. Sometimes similar phrases in Python insist on careful consideration of context.

But at this point, we haven’t tried using a keyword argument. So try changing line 5 in the previous Python code to the following.

ourFunc(x=1, 2, 3)

This type of call would work in some languages. But, here, it brings up the syntax error. Note that the following will still bring up that same error message.

ourFunc(1, y=2, 3)

The old saying about putting the cart before the horse strikes again as Python’s interpreter will still complain about the 3 following y=2. It’s not enough to have one instance of a positional argument precede the keyword argument.

How To Fix the Error

Understanding the nature of this error can be somewhat complex. But, thankfully, the actual solution is fairly simple. We simply need to remember the cart before the horse analogy and shuffle things around a little. Try running the following code.

def ourFunc(x, y, z):
ourList = [1,2,3,4,5,6]
print(“x=” + str(x) + ” y=” + str(y) + ” z=” + str(z) + ” ourList=” + str(ourList[z]))

ourFunc(1, 2, z=3)

Everything will run smoothly now and won’t produce an error message. This is because we’re putting the positional arguments in front of the keyword arguments. However, this isn’t the only way to fix the error. This example is obviously simple by design since it’s intended to be an explanatory sample. But even in a more complex real-world scenario you should take a step back from your code and consider the underlying design. It may be enough to simply pass the arguments to the function without using any keywords. So, for example, you might just call ourFunc with something like the following.

ourFunc(1, 2, 3)

Again, this specific fix is simple due to the code’s spartan context. Real-world examples can get a little messier over the course of long design periods or updates. But you may well be able to simply remove the inherent complexity of mixed positional and keyword arguments in your code to fix this error.

How to Fix the Python Error: SyntaxError: positional argument follows keyword argument
Scroll to top