In Python, functions have a default, non-default argument value. The default argument (x=‘name’) is defined in a function with the fallback value as a default parameter. The non-default argument (x) value is mandatory to define as a parameter while accessing the function. The non-default argument can not follow the default argument because it raises SyntaxError.
This write-up will provide you with reasons, and various solutions for the error “non-default argument follows default argument” in Python. The content explained in this guide is as follows:
- Reason: Positional Parameter Follows Default Parameter
- Solution 1: Specify Default Parameter After the Positional Parameter
- Solution 2: Follow the Default Function Argument Order
Let’s begin with the reason first!
Reason: Positional Parameter Follows Default Parameter
The prominent reason which invokes this error in Python is when a positional/non-default parameter in a function follows a default parameter of a function. Look at the following snippet:
The above snippet shows the “SyntaxError” because the positional parameter specifies after the default parameter.
Solution 1: Specify Default Parameter After the Positional Parameter
To fix this error, specify the default argument after the non-default argument of the function. An example of code is given below:
Code:
def students(name, height, age='18'):
print(name)
print(height)
print(age)
return 2+3
studt = students('Lily', 100)
print(studt)
In the above code, the function “students” is defined in the program. The function “students” has three parameters, two non-default arguments, “name” and “height”, and one default argument, “age=18”. The default argument is specified after the non-default argument to avoid any errors.
Output:
The above output successfully shows the function’s return value because the default argument has been specified at the end.
Solution 2: Follow the Default Function Argument Order
By following the default function argument order, we can rectify this “SyntaxError: non-default argument follows default argument” in Python. The function argument order will be listed below:
- Non-default arguments or positional arguments
- Default arguments or keyword argument
- Python *args (keyword-only arguments)
- Python **kwargs (variable-keywords arguments)
Note: The Python *args and **kwargs are used to specify the argument value after the default argument in the function. The Python *args are used to specify keyword-only arguments, and Python **kwargs are used to specify variable keyword arguments. Let’s understand it via the following code:
Example: Initializing Function Argument in Order
If the default function argument order is followed while initializing the function argument in Python, the “SyntaxError” will not appear in the output. Let’s have a look at the given below example code:
Code-1:
def students(name, height, age='18', *args, **kwargs):
print('name: ',name)
print('height: ',height)
print('age: ',age)
print('args: ',args)
print('kwargs: ',kwargs)
return 2+3
studt = students('Lily', 20, 18, 'Alex', Gender='Male')
print(studt)
In the above code, the non-default, default, *args and **kargs function arguments are initialized in order. The argument values are printed inside the function using the “print()” function. The argument value is passed inside the parentheses while accessing the function at the end.
Output:
The above output shows the value of all arguments without any SyntaxError because the function argument is placed in proper order.
But in the above scenario, the default value must be passed while passing the argument value of the function inside the parentheses. Otherwise, the output will be confusing and incorrect. To overcome this, it is recommended to use the “**kwargs” variable-keyword argument rather than only the “*args” keyword argument. Let’s have a look at the below code to clarify the concept:
Code-2: (Using Only Variable-Keyword Argument)
def students(name, height, age='18', **kwargs):
print('name: ',name)
print('height: ',height)
print('age: ',age)
print('kwargs: ',kwargs)
return 2+3
studt = students('Lily', 20, Boy_Name='Alex', Gender='Male')
print(studt)
In the above code, the non-default, default, and variable-keyword (**kargs) arguments are passed inside the function parentheses. The argument’s values are passed while accessing the function by placing it inside its parentheses.
Output:
In the above output, it is verified that the correct value of “Default Argument” and all other arguments are created successfully.
Conclusion
The “non-default argument follows default argument” syntax error arises when a positional/non-default parameter in a function follows a default parameter of a function. To rectify this error specify the default arguments after the non-default arguments in a function. This error also resolves by using Python “*args” and “**kwargs” and following the function argument order. This article explained why a “non-default argument follows a default argument” syntax error happens and how it can be fixed.