This error occurs when you define a function with a non-default parameter following a default parameter. If positional parameters follow a default parameter, the Python interpreter cannot know if you passed an argument for the default parameter or for one of the positional parameters during the function call.
You can solve this error by ensuring all the default arguments come after the positional arguments of the function. For example,
def hello(name, message="Hi"): print(message, name, '! You are learning Python!') name = input("What is your name?") hello(name)
This tutorial will go through the error in detail and solve it with an example.
Table of contents
SyntaxError: non-default argument follows default argument
What is a SyntaxError?
The term syntax refers to the rules that define the various combinations of symbols to instruct a computer to perform tasks. A syntax error violates the syntax rules for a given programming language, and a syntax error is similar to a grammatical error in human languages.
When you run a Python program, the interpreter will parse it to convert the code into Python byte code to execute it. If there is invalid syntax in the Python code during the parsing stage of execution, the interpreter will throw a SyntaxError.
For further reading on SyntaxError involving arguments, go to the article: How to Solve Python SyntaxError: positional argument follows keyword argument.
What is a Default Argument in Python?
Python allows for function arguments to have default values. If you call a function without specifiying an argument, the function uses the default. You can assign a default value using the assignment (=) operator with the following syntax:
keyword = value
Let’s look at an example of a function with a default argument.
def multiplier(first_number, second_number=5): print(f'{first_number} multiplied by {second_number} is {first_number * second_number}')
In the above code, we specify a positional parameter called first_number
and a default called second_number
.
When we call the multiplier function to multiply numbers, the function will use the default argument if we do not specify an argument for second_number
.
Let’s call the function with and without specifying an argument for the second_number
parameter.
multiplier(2) multiple(2,3) multiplier(2, second_number = 3)
2 multiplied by 5 is 10 2 multiplied by 3 is 6 2 multiplied by 3 is 6
- In the first call we leave out the
second_number
argument, therefore the function uses the default value 5. - In the second call, we use only positional arguments, so the function uses those values instead of the default value.
- In the third call, we use a positional argument followed by a key argument, so the function does not use the default value of 5 and uses 3 instead.
Example
Let’s look at an example that will raise the SyntaxError:
def hello(message="Hi", name): print(message, name, '! You are learning Python!')
In the above code, we define a function to greet a user. The function has a default parameter called message
and a non-default argument called name. The program takes the user’s name using the input()
function. We can then call the hello()
function with the name argument.
name = input("What is your name?") hello(name)
Let’s run the code to see what happens:
def hello(message="Hi", name): ^ SyntaxError: non-default argument follows default argument
The code raises the SyntaxError because the default argument comes before the non-default argument in the function definition.
Solution
The correct order for defining parameters in a function is:
- Positional or non-default parameters, e.g.
(x, y, z)
- Keyword or default parameters, e.g.
(a='b', c='d')
- Keyword-only parameters, e.g.
*args
- Var-keyword parameters, e.g.
**kwargs
We must ensure the default parameter comes after the non-default parameter to solve this error.
Let’s look at the revised code:
def hello(name, message="Hi"): print(message, name, '! You are learning Python!') name = input("What is your name?") hello(name)
Let’s run the code to get the output:
What is your name? Jim Hi Jim ! You are learning Python!
The code runs successfully and prints the greeting string with the name from the user input to the console.
Summary
Congratulations on reading to the end of this tutorial! The error “SyntaxError: non-default argument follows default argument” occurs when you specify a default argument before a non-default argument.
To solve this error, ensure that you arrange all arguments in your function definition such that the default arguments come after the non-default arguments.
To learn more about Python for data science and machine learning, go to the online courses page on Python for the most comprehensive courses available.
Have fun and happy researching!
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.