Fix SyntaxError: non-default argument follows default argument in Python

Fix SyntaxError: non-default argument follows default argument in Python

In Python, the syntax for defining a function is strict, and there are specific rules to be followed when writing the function arguments. One of the most common mistakes that developers make while writing functions is the use of non-default arguments after the default arguments. This can result in a SyntaxError: non-default argument follows default argument error message. In this tutorial, we will discuss why this error occurs, how to resolve it, and provide multiple examples to help you better understand the concept.

Why Does This Error Occur?

The error message SyntaxError: non-default argument follows default argument in Python occurs when you try to define a function with a non-default argument (an argument that doesn’t have a default value) after a default argument (an argument that has a default value). This is because Python requires that all default arguments be placed after any non-default arguments. In other words, all non-default arguments should appear first, and only then should the default arguments be declared.

The main reason for this rule is that the arguments of a function are matched in the order in which they are defined, not by their names. As a result, default arguments must come after non-default arguments so that the non-default arguments can be matched first and the default arguments can be matched only if the corresponding non-default argument is not supplied.

For example, consider the following code:

def my_function(arg1=0, arg2):
    print(arg1, arg2)

my_function(arg2=2, arg1=1)

When you run this code, you will get the following error:

SyntaxError: non-default argument follows default argument

This is because arg2 is a non-default argument, and it is declared after arg1, which is a default argument.

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

To resolve the SyntaxError: non-default argument follows default argument in Python, you need to move the default arguments to the end of the argument list. In other words, all non-default arguments should appear first, and only then should the default arguments be declared.

Here is an example of how to correct the previous code:

def my_function(arg2, arg1=0):
    print(arg1, arg2)

my_function(arg2=2, arg1=1)

In this example, the non-default argument arg2 is declared first, and the default argument arg1 is declared second. When you run this code, the output will be 1 2.

Examples with Explanations

In the following examples, we will look at different scenarios of how to resolve the SyntaxError: non-default argument follows default argument.

Example 1: Multiple Non-Default Arguments

Consider the following code:

def my_function(arg1, arg2, arg3=0, arg4=1):
    print(arg1, arg2, arg3, arg4)

my_function(1, 2)

In this example, arg1 and arg2 are non-default arguments, and arg3 and arg4 are default arguments. The order of the arguments is correct, and when you run the code, the output will be 1 2 0 1.

Example 2: Mix of Non-Default and Default Arguments

Consider the following code:

def my_function(arg1, arg2=0, arg3, arg4=1):
    print(arg1, arg2, arg3, arg4)

my_function(1, arg3=2)

In this example, arg1 is a non-default argument, arg2 and arg4 are default arguments, and arg3 is a non-default argument. The order of the arguments is incorrect, and when you run the code, you will get the following error:

SyntaxError: non-default argument follows default argument

To resolve this error, you need to move the non-default argument arg3 to the beginning of the argument list. Here is the corrected code:

def my_function(arg3, arg1, arg2=0, arg4=1):
    print(arg1, arg2, arg3, arg4)

my_function(2, 1)

In this corrected code, the order of the arguments is correct, and when you run the code, the output will be 1 0 2 1.

Example 3: Default Arguments Only

Consider the following code:

def my_function(arg1=0, arg2=1, arg3=2, arg4=3):
    print(arg1, arg2, arg3, arg4)

my_function()

In this example, all of the arguments are default arguments. The order of the arguments is correct, and when you run the code, the output will be 0 1 2 3.

Example 4: Non-Default Arguments Only

Consider the following code:

def my_function(arg1, arg2, arg3, arg4):
    print(arg1, arg2, arg3, arg4)

my_function(1, 2, 3, 4)

In this example, all of the arguments are non-default arguments. The order of the arguments is correct, and when you run the code, the output will be 1 2 3 4.

Conclusion

In this tutorial, we discussed the SyntaxError: non-default argument follows default argument error message in Python and how to resolve it. We covered why this error occurs, and we provided multiple examples to help you better understand the concept.

Remember, the rule is that all non-default arguments should appear first, and only then should the default arguments be declared. This rule ensures that the arguments of a function are matched in the order in which they are defined, not by their names.

By following this rule, you can avoid the SyntaxError: non-default argument follows default argument error message and write correct and error-free Python code.

See also: Syntaxerror: Cannot use import statement outside a module

FAQs

What is the SyntaxError: non-default argument follows default argument error in Python?

This error occurs in Python when a default argument (i.e., an argument with a default value) is followed by a non-default argument (i.e., an argument without a default value) in the function definition. This is not allowed in Python, and the error message is raised to alert you to this problem.

Why does the SyntaxError: non-default argument follows default argument error occur in Python?

This error occurs because the order of arguments in a Python function definition is significant. Non-default arguments must come before default arguments. If a default argument is placed before a non-default argument, it will cause a SyntaxError.

How can I resolve the SyntaxError: non-default argument follows default argument error in Python?

To resolve this error, you need to rearrange the order of the arguments in the function definition so that all non-default arguments come before any default arguments. This ensures that the arguments of the function are matched in the order in which they are defined, not by their names. Once you have corrected the order of the arguments, you can run your code again, and the error should no longer occur.