In a Python function definition, we can set default values for arguments. Specifying an argument value during a function call is optional when you set a default argument value. You must define the default arguments after the positional or non-default arguments. Otherwise, you will raise the error “SyntaxError: non-default argument follows default argument”.

This tutorial will go through the error in detail and solve it with an example.


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 an argument value, the function uses the default value. 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 and a non-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 non-default or positional argument called first_number and a default or keyword argument called second_number. When we call the multiplier function to multiply numbers, the function will use the default function if we do not specify a value to assign to second_number.

Let’s call the function where we specify we either specify or do not specify a second_number value:

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!')

name = input("What is your name?")

hello(name)

In the above code, we define a function to greet a user. The function takes a default argument 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. 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

We must ensure the default argument comes after the non-default argument 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!