Python SyntaxError: non-default argument follows default argument error is an error you might run into while dealing with functional arguments. The non-default argument follows the default argument error of python is a type of SyntaxError that is raised when the default argument is mentioned before the non-default argument. This type of error is pretty difficult to locate and resolve.
In this guide here you will get all the information required to resolve the SyntaxError: non-default argument follows default argument error.
Contents
- 1 What is the default argument?
- 2 What is the non-default argument?
- 3 What is python SyntaxError: non-default argument follows default argument error?
- 4 Causes for python SyntaxError: non-default argument follows default argument error
- 5 Solution for SyntaxError: non-default argument follows default argument error
- 6 FAQs
- 7 References
What is the default argument?
The default arguments are basically the arguments that do not need to be defined during a function and will provide a default value. The default value indicates that the function argument will accept the value if the non-argument value is accepted during the function call.
Example:
What is the non-default argument?
The non-default arguments are used to pass the default arguments and to provide a variable for the default argument values.
Example:
What is python SyntaxError: non-default argument follows default argument error?
The python non-default argument follows default argument error arises when the function is defined with a positional parameter. Here the positional parameter follows a default parameter. This is a type of SyntaxError where the default argument is specified before a non-default argument.
Causes for python SyntaxError: non-default argument follows default argument error
As mentioned above the python SyntaxError: non-default argument follows default argument error is raised when the default argument is specified before the non-default argument error.
This happens because the default arguments require a non-default argument to be specified. If we specify the non-default argument afterward the ordeal would break and the code will raise the python SyntaxError: non-default argument follows default argument error.
Another reason for such an error is mentioning the keyword arguments before the non-keyword arguments which are pretty similar to the above cause.
Apart from that if you keep more than one string in a function or keep the variables in different formats then it might raise an error.
Below is an example of the SyntaxError: non-default argument follows default argument error.
Syntax:
def a(len1,hgt=len1,til,col=0):
system('mode con cols='+len1,'lines='+hgt)
system('title',til)
system('color',col)
a(56,55,"ok","9n")
input()
Solution for SyntaxError: non-default argument follows default argument error
The only solution for SyntaxError: non-default argument follows default argument error is to define the arguments properly, that is defining the non-default arguments before the default arguments.
1. Mention the Parameters properly
For that, you need to put the positional or the non-default parameters first that is assign a variable. Then to the non-default parameters add the default parameters which are some values to the variables. If we do that then even if the functions would be declared with one or more default parameter values the SyntaxError: non-default argument follows default argument error will not arise.
2. Using one string of parameters at once
Sometimes the os. systems raise the SyntaxError: non-default argument follows default argument error if there is more than one string parameter so for that you need to mention only one string parameter as it only accepts one at a time.
def a(len1, hgt, til, col=0):
system('mode con cols=%s lines=%s' % (len1, hgt))
system('title %s' % til)
system('color %s' % col)
3. Place a non-keyword argument before a keyword argument
Similar to the default and non-default parameters the keywords also cause the SyntaxError: non-default argument follows default argument error if the keyword argument is placed after the non-keyword argument. So to resolve this SyntaxError: non-default argument follows default argument error you need to define the non-keyword argument first and then the non-keyword argument.
Syntax:
def a(len1,til,hgt=len1,col=0):
system('mode con cols='+len1,'lines='+hgt)
system('title',til)
system('color',col)
a(56,"ok",55,"9n")
4. Keep all the variables in the same format
In some instances the SyntaxError: non-default argument follows default argument error arises when the variables are present in different formats. In order to resolve this kind of error, you need to put them in the same format. For example, if there are some variables mentioned in a function and you mentioned only two of their values then it might raise the SyntaxError: non-default argument follows default argument error so instead, you can try to add values for all the variables or do not add any values to any of the variables.
Syntax:
def(len1, hgt, til, col):
Or
def(len1=value, hgt=value, til=value, col=value):
FAQs
What are keywords-only parameters?
These are used to prompt the user to state the keyword used in the already-defined function when making a call to that function. This parameter gives you access to rename the parameters and bounds the users to call the functions by specifying arguments by only their positions.
These parameters should be mentioned with a ‘*’ marker and followed with a ‘/’ at the end. It is used as *arg for the keyword arguments.
What are Var- keyword parameters?
These are known as non-keyword parameters or variable-length keyword arguments. These are used to pass a keyworded variable-length argument list. In this parameter you provide names to the variables when it pass the function.
It is defined with double * i.e., ‘**’ or ‘**kwargs’.
References
Master day-to-day error handling by following PythonClear errors.