Python SyntaxError: non-default argument follows default argument Solution

Posted in /  

Python SyntaxError: non-default argument follows default argument Solution
vinaykhatri

Vinay Khatri
Last updated on April 20, 2024

    While defining arguments in a function definition, we can set default argument values, making the argument optional during the function calls. When defining the default arguments, we have to follow the proper syntax. All the default arguments must be defined after the positional or non-default arguments. Otherwise, we will receive the SyntaxError: non-default argument follows default argument Error.

    In this Python guide, we will discuss the SyntaxError: non-default argument follows default argument Error in detail and see how to solve it. So let's get started with the Error Statement

    The Python SyntaxError: non-default argument follows default argument

    In Python, when we define arguments during a function definition, we can either use the argument names only or pass some default values to the arguments.

    Example

    def greet(user, message="hello"):
        print(message, user)

    In the above example the user and message are two argument names defined during the function definition greet .

    Between these two arguments user is the non-default positional argument, and message is the default argument with a value "hello" .

    When we try to define non-default and default arguments in a function definition, we need to write the non-default arguments before the default arguments. And if we specify the default arguments before the non-default or positional arguments, Python throws the error SyntaxError: non-default argument follows default argument The Error Statement SyntaxError: non-default argument follows default argument can be divided into two parts

    1. SyntaxError  (Exception Type)
    2. non-default argument follows default argument (Error Message)

    1. SyntaxError

    Python is a programming language, and there is a proper syntax or structure that has been defined to write a Python program. If we try to write a code in Python that does not fit in the Python-specified syntax, the interpreter throws the SyntaxError.

    There is a syntax already defined for Python on how to define a function and how the arguments must be specified, and in what order. And when we do not follow the correct syntax, the interpreter raises the SyntaxError.

    2. non-default argument follows default argument

    This error message is raised when we try to specify a default argument before a non-default argument in the function definition.

    Example

    Let's define a function and pass the default argument before the non-default one.

    def greet(message ="Hello! How are you doing?", name):
        print(name, message)
    
    name = "Anil"
    
    greet(name)

    Output

    File "main.py", line 1
        def greet(message="Hello! How are you doing?", name):
                                                             ^
    SyntaxError: non-default argument follows default argument

    The output of the above example is what we expected. While defining the greet() function we defined the message argument as a default argument before the name argument. And according to the Python syntax, if we specify a default argument before a non-default argument, we will receive the SyntaxError.

    Solution

    The solution to the above problem is straightforward, whenever you see a similar error in your output, all you need to do is put the default arguments after the non-default argument.

    Example Solution

    def greet(name,messaage ="Hello! How are you doing?"):
        print(name, messaage)
    
    name = "Anil"
    
    greet(name)

    output

    Anil Hello! How are you doing?

    Wrapping Up!

    In this Python tutorial, we discussed Python "SyntaxError: non-default argument follows default argument" Error. This error is raised in a Python program when we specify a default argument before a non-default positional argument.

    To solve this error, we need to make sure that in the function definition, we specify the non-default argument before the default ones.

    People are also reading:

    Leave a Comment on this Post

    0 Comments