SyntaxError Non-default Argument Follows Default Argument Error

SyntaxError: non-default argument follows default argument error occurs when you are dealing with functional arguments. This is a syntax error that usually occurs when the default argument is declared before the non-default argument. This problem seems to be complex but can be solved easily. In this short article, we will discuss how we can solve SyntaxError: non-default argument follows default argument error using various techniques. Moreover, we will also discuss how to understand errors in Python so that in the future you can easily understand and solve the errors:

How to Solve SyntaxError: non-default argument follows default argument in Python?

The SyntaxError: non-default argument follows default argument is a type of syntax error that occurs when dealing with arguments of a function. In Python, there is a special order for default and non-default arguments. If you declared the default argument before then the non-default argument, then you will get the error.

Before solving the error, let us first understand what is default and non-default arguments:

What is the default argument in Python?

The default argument in Python is an argument that has a default value and the function will be executed even if the arguments are not provided while calling it. The following function contains default arguments:

# defining a function
def main(a = 10, b = 10):
    
    return a+b

# calling the function
main()

The above code will run without giving an error because the function will take the default values for the given parameters.

What is a non-default argument in Python?

A non-default argument in Python is an argument whose value is not defined while declaring the function. The value can be only passed while calling the function and must be provided otherwise we will get an error. Consider the following example:

# defining a function
def main(a , b ):
    
    return a+b

# calling the function
main()

Output:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_206558/4156337786.py in <module>
      5 
      6 # calling the function
----> 7 main()

TypeError: main() missing 2 required positional arguments: 'a' and 'b'

Notice that the error clearly says, we need to provide values for the two arguments because they are non-default.

Why do we get SyntaxError: non-default argument follows default argument?

Usually, the reason for getting the error is that we used the default argument before the non-default arguments. For example, consider the following example:

# defining a function
def main(a=10 , b ):
    
    return a+b

# calling the function
main(10)

Output:

 File "/tmp/ipykernel_206558/1645757424.py", line 2
    def main(a=10 , b ):
                    ^
SyntaxError: non-default argument follows default argument

As you can see, we got the error because the default argument is followed by the non-default argument which is not allowed in Python. Always the non-default argument should be before the default argument.

The solution to the error

Now we will discuss the possible solutions to the error. We already know that the error is because of the position of default and non-default parameters.

SyntaxError: non-default argument follows default argument

The best way to solve the error is to fix the positions of the parameters. If you have default and non-default parameters in your function. Then make sure that the default parameter always comes after the non-default parameters as shown below:

# defining a function
def main(a , b=10 ):
    return a+b
# calling the function
main(10)

As you can see, the default parameter is after the non-default one.

Understanding the SyntaxError: non-default argument follows default argument

In Python errors, usually have two main parts. The first part of the error gives information about the category of the error. For example, in this case, the error belongs to the SyntaxError category. A Syntax error is simply a mistake in the use of Python language. It can be either typo, an extra comma, etc.

The second part of the error in Python gives more specific information about the error. For example, in this case, the error says that the non-default argument follows the default argument which simply means we have put the default parameter values before the non-default one.

Difference between parameters and arguments in Python

A parameter is simply the variable that is listed inside the round bracket when we declare the function. While arguments are the actual values of those parameters that we used while calling the function.

For example, the following is an example of parameters in Python:

# defining a function
def main(a , b ):
    return a+b

Here, a and b are known as parameters.

Here is an example of an argument:

# calling the function
main(10, 3)

The 10 and 3 are known as arguments.

Conclusion

In this short article, we discussed how we can solve SyntaxError error in Python. We discussed the main reasons for getting the error. Moreover, we also discuss how to understand errors in Python

Related Errors

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top