Syntaxerror: non-default argument follows default argument

If you are working with Python functionals argument, you probably encounter the syntaxerror: non-default argument follows default argument.

So if you are confused about how to fix this error, don’t worry! This article got your back!

In this article, we will explore how to troubleshoot this syntax error.

Continue to read to have a better understanding of why does syntaxerror non default argument follows default argument occurs.

What is “syntaxerror non default argument follows default argument” in Python?

The syntaxerror: non-default argument follows default argument occurs when you define an argument with a non-default value after an argument with a default value in a function definition.

In other words, when you are trying to define the function with a positional parameter that goes after with default parameter.

For example:

def sample(website="Itsourcecode", visitors):
    print(f"Hello welcome to, {website}! We have more {visitors} this month.")

sample("Itsourcecode", 5000000)

If you try to run this code, as a result, it will throw an error message:

C:\Users\pies-pc2\PycharmProjects\pythonProject\main.py 
  File "C:\Users\pies-pc2\PycharmProjects\pythonProject\main.py", line 1
    def sample(website="Itsourcecode", visitors):
                                       ^^^^^^^^
SyntaxError: non-default argument follows default argument

In Python, all required parameters must be placed before any default arguments because they are mandatory, whereas default arguments are not.

Are you still confused about non default argument and default argument? Here you’ll see the distinction between the two arguments.

What is non-default argument?

A non-default argument is an argument in a function definition that does not have a default value. This means that when calling the function, a value must be provided for this argument.

Non-default arguments are also known as required or positional arguments.

For example:

(x,y,z)

In a simple word, non-default argument does not have a default value, so the caller must always provide a value for them.

For example:

def sample(website, message):
    print(greeting, website)

As you can see, both website and message are non-default arguments, and the caller should provide values for both of them.

What is default argument?

A default argument is an argument in a function definition that has a default value. This means that when calling the function, if no value is provided for this argument, the default value will be used.

Default arguments are also known as optional arguments.

For example:

(x='10', y = '20', z='30')

Moreover, default arguments is an argument that is defined with an equal sign (=) followed by the default value.

def sample(website, message="Hi, welcome to"):
print(message, website)

As you can see in our provided example, message is a default argument with the default value “Hi”. If you don’t provide a value for message, the function will use “Hi” as its value.

Why does “syntaxerror non default argument follows default argument” error occur?

This error can occur due to several factors, which include the following:

Incorrect order of arguments

def calculate_total(price, quantity=10, discount):
    total = price * quantity * (1 - discount)
    return total

# Function call
calculate_total(100, 10, 0.5)

The function calculate_total is defined with a default argument quantity and a non-default argument discount. However, the non-default argument discount appears after the default argument quantity.

When we try to call the function with specific values (calculate_total(100, 10, 0.5)), Python encounters the error because it expects non-default arguments to come before default arguments.

Corrected code:

def calculate_total(price, discount, quantity=10):
    total = price * quantity * (1 - discount)
    return total

# Function call
print(calculate_total(100, 0.5, 10))

In the corrected code order of the arguments in the calculate_total function has been corrected.

The non-default argument discount now appears before the default argument quantity.

So when you call the function, it will correctly calculate the total and return the value.

Output:

500.0

Missing arguments in function call

def sample(website="Itsourcecode", visitors):
    print(f"Hello welcome to, {website}! We have reach more than {visitors} click this month.")

sample("Itsourcecode", 5000000)

We have the def sample that takes two arguments: website (with a default value of “Itsourcecode”) and visitors.

However, the error arises because the non-default argument visitors appears after the default argument website.

When we try to call the sample function with a specific website and visitors (sample(“website”, 5000000)), Python encounters the error because it expects non-default arguments to come before default arguments.

Corrected code:

def sample(visitors, website="Itsourcecode"):
    print(f"Hello welcome to, {website}! We have reach more than {visitors} click this month.")

sample(5000000, "Itsourcecode")

As you can see, we simply rearranged the order of the arguments in the def sample function. By placing the non-default argument visitors before the default argument website, we ensure that the function call sample(5000000, “Itsourcecode”) works correctly.

Output:

Hello welcome to, Itsourcecode! We have reach more than 5000000 click this month.

How to fix “syntaxerror: non-default argument follows default argument”?

To fix the syntaxerror non-default argument follows default argument error, you have to reorder the arguments in your function definition so that all non-default arguments come before any default arguments.

This is because all required parameters must be placed before any default arguments.

It is hard for the interpreter to decide which values match which arguments if mixed modes were allowed.

Here are the following solutions that you can use to resolve the error:

1. Reorder the arguments

You have to reorder the arguments so that all non-default arguments come before default arguments.

For example:

def sample_function(x, y, z=15):
    print(x, y, z)

sample_function(5, 10)

As you can see, we reordered the arguments so that the non-default arguments “x” and “y” come before the default argument “z.”

When we call the function with two arguments, the first argument is assigned to “x,” the second argument is assigned to “y,” and “z” takes its default value of “15.”

Output:

5 10 15

2. Use function with keyword-only arguments

You can use the “*” character in the function definition so that the following arguments are keyword-only arguments.

It enables you to have default arguments prior to non-default arguments without arising syntax errors.

For example:

def sample(cliertfirst_name, clientlast_name, *, age=None, address=None, gender = None):
    print(f"Fullname: {cliertfirst_name} {clientlast_name}")
    if age:
        print(f"Age: {age}")
    if address:
        print(f"Address: {address}")
    if gender:
            print(f"Gender: {gender}")

# Calling the function
sample("Caren", "Love", age=18, address="Canada", gender="female")

Output:

Fullname: Caren Love
Age: 18
Address: Canada
Gender: female

3. Use keyword arguments when calling the function to specify the values of the arguments

Alternatively, you can use the keyword arguments when calling the function to specify the values of the arguments.

For example:

def my_function(x=5, y=10, z=15):
    print(x, y, z)

my_function(z=30)

As you have noticed in this example, we have defined a function with three default arguments.

When we call the function using a keyword argument z=15, we are specifying that we want to override the default value of z with the value 30.

The other arguments take their default values. It enables you to have default arguments prior to non-default arguments without arising syntax errors.

Output:

5 10 30

Conclusion

In conclusion, the error message syntaxerror: non-default argument follows default argument occurs when you define an argument with a non-default value after an argument with a default value in a function definition.

We already discussed above what this error is all about and multiple ways to resolve this error.

By executing the solutions above, you can master this Syntaxerror in Python with the help of this guide.

Here are the things that you need to avoid this syntax error.

  1. You should place a non-default argument before default arguments in your function definition.
  1. If ever you want to provide a default value for an argument that needs to appear before a non-default argument, you can use the “*” character in the function definition.

You could also check out other “SyntaxError” articles that may help you in the future if you encounter them.

We are hoping that this article helps you fix the error. Thank you for reading itsourcecoders 😊

Leave a Comment