SyntaxError: positional argument follows keyword argument

avatar

Borislav Hadzhiev

Mon Apr 25 20223 min read

banner

Photo by Kalen Emsley

SyntaxError: positional argument follows keyword argument #

The Python "SyntaxError: positional argument follows keyword argument" occurs when we pass keyword arguments before positional ones in a call to a function. To solve the error, pass positional arguments in the right order and pass any keyword arguments after the positional ones.

syntaxerror positional argument follows keyword argument

Here is an example of how the error occurs.

main.py
def get_employee(first, last, salary): return {'first': first, 'last': last, 'salary': salary} # ⛔️ SyntaxError: positional argument follows keyword argument get_employee('Alice', last='Smith', 100)

Passing a positional argument to a function looks like 'Alice',, whereas passing a keyword argument to a function looks like last='Smith',.

The error was caused because we passed a keyword argument last, and then provided a value for the salary positional argument.

If you specify keyword arguments before positional ones, Python has no way of knowing a value for which positional argument we are providing.

One way to solve the error is to only pass positional arguments to the function.

main.py
def get_employee(first, last, salary): return {'first': first, 'last': last, 'salary': salary} # ✅ only passing positional arguments to the function result = get_employee('Alice', 'Smith', 100) print(result) # 👉️ {'first': 'Alice', 'last': 'Smith', 'salary': 100}

The example above only uses positional arguments. The function knows for which parameter we passed a value based on the order of the positional arguments.

Alternatively, you can only pass keyword arguments to the function.

main.py
def get_employee(first, last, salary): return {'first': first, 'last': last, 'salary': salary} result = get_employee(salary=100, first='Alice', last='Smith') print(result) # 👉️ {'first': 'Alice', 'last': 'Smith', 'salary': 100}
We only passed keyword arguments in the function call. Python knows for which parameter we passed a value based on the name of the keyword argument, e.g. salary=100.

You can also pass positional and keyword arguments in the same function call. Note that keyword arguments must follow positional ones.

main.py
def get_employee(first, last, salary): return {'first': first, 'last': last, 'salary': salary} result = get_employee('Alice', salary=100, last='Smith') print(result) # 👉️ {'first': 'Alice', 'last': 'Smith', 'salary': 100}

We used a positional argument (Alice) and 2 keyword arguments - (salary=100 and last='Smith').

Make sure that any positional arguments you specify in the function call are passed in the correct order (the order must correspond to the parameter list in the function's definition).

The order you pass the keyword arguments in doesn't matter, as long as they come after the positional ones.

Make sure that you aren't just blindly moving the keyword arguments after all of the positional ones, because the order of the positional arguments matters.

Here is an example of how this can go wrong.

main.py
def get_employee(first, last, salary): return {'first': first, 'last': last, 'salary': salary} # ⛔️ TypeError: get_employee() got multiple values for argument 'last' result = get_employee('Alice', 100, last='Smith')

The keyword argument in the example comes after any positional ones, but we still got an error.

The reason we got the error is because we used positional arguments to pass a value for the first and last parameters and then passed a value for the last parameter using a keyword argument.

The easiest way to solve the error in the example is to pass only positional arguments (in the correct order) to the function.

The same is the case when a function definition has default parameters - it has to define its default parameters after the positional ones.

main.py
# 👇️ default parameter `last='Doe'` def get_employee(first, salary, last='Doe'): return {'first': first, 'last': last, 'salary': salary} # 👇️ not passing value for default parameter emp_1 = get_employee('James', 100) print(emp_1) # {'first': 'James', 'last': 'Doe', 'salary': 100} # 👇️ passing value for default parameter emp_2 = get_employee('Alice', 100, 'Smith') print(emp_2) # {'first': 'Alice', 'last': 'Smith', 'salary': 100}

Default parameter values are ones that have the form parameter = expression.

When we declare a function with one or more default parameter values, the corresponding arguments can be omitted when the function is invoked.

Positional parameters cannot follow a parameter with a default value because otherwise Python has no way of knowing if we passed an argument for the default parameter or for a positional one.

Use the search field on my Home Page to filter through my more than 1,000 articles.