SyntaxError non-keyword arg after keyword arg

In Python while dealing with arg function you may face is the “SyntaxError non-keyword arg after keyword arg.” This error occurs when you pass a non-keyword or positional argument, after a keyword argument in a function call. This article will give you a walkthrough about this error, what causes it, and how to fix it along with some frequently asked questions related to this error.

By the end of this article, you can avoid and resolve this error in your Python code.

What is the “SyntaxError non-keyword arg after keyword arg”?

The “SyntaxError non-keyword arg after keyword arg” is a syntax error that occurs when a function call has a non-keyword argument (also known as a positional argument) after a keyword argument. A keyword argument is an argument that is specified by using the name of the parameter and an equal sign, such as name= “Alice”.

A non-keyword argument is an argument that is passed without specifying the name of the parameter, such as “Alice”. For example, consider the following function definition and function call:

!pip install rich
from rich import print

def greet(name, message):
    print(f"Hello, {name}! {message}")

greet(name="Alice", message="How are you?")

The function greet takes two parameters: name and message. The function call passes two arguments: name= “Alice” and “How are you?”. The first is a keyword argument, specifying the parameter’s name. The second argument is a non-keyword argument, which does not specify the parameter’s name. This function call will result in the following error:

SyntaxError: non-keyword arg after keyword arg
SyntaxError: non-keyword arg after keyword arg

The error message indicates that the function call has a non-keyword argument after a keyword argument, which is not allowed in Python.

What causes the “SyntaxError non-keyword arg after keyword arg”?

The main cause of the “SyntaxError non-keyword arg after keyword arg” is violating the order of arguments in a function call. In Python, the order of arguments in a function call is important, as it determines how the arguments match the function’s parameters.

The general rule is that non-keyword arguments must come before keyword arguments, and keyword arguments can come in any order. For example, the following function calls are valid, as they follow the rule:

greet("Alice", message="How are you?") # non-keyword argument before keyword argument
greet(message="How are you?", name="Alice") # keyword arguments in any order

However, the following function call is invalid, as it violates the rule:

greet(name="Alice", message="How are you?")

Python does not allow non-keyword arguments after keyword arguments because it would create ambiguity and confusion. For example, consider the following function definition and function call:

def greet(name, message, age):
    print(f"Hello, {name}! {message} You are {age} years old.")

greet(name="Alice", "How are you?", 25)

The function greet takes three parameters: name, message, and age. The function call passes three arguments: name= “Alice”, “How are you?”, and 25. The first is a keyword argument, specifying the parameter’s name. The second and third arguments are non-keyword arguments, as they do not specify the parameter’s name.

However, it is not clear which parameter the second and third arguments are matched with. Is the second argument matched with the message or age? Is the third argument matched with the message or age? Python cannot determine the correct matching, raising the error.

How do we resolve the “SyntaxError non-keyword arg after keyword arg”?

The simplest way to resolve the “SyntaxError non-keyword arg after keyword arg” is to follow the order of arguments in a function call. As mentioned earlier, the rule is that non-keyword arguments must come before keyword arguments, and keyword arguments can come in any order. For example, to fix the previous function call, we can either use all keyword arguments or use a non-keyword argument for the second argument:

greet(name="Alice", message="How are you?", age=25) # all keyword arguments
greet("Alice", "How are you?", age=25) # non-keyword argument for the second argument

Another way to resolve the error is to use the * operator to unpack a sequence of non-keyword arguments after a keyword argument. The * operator allows us to pass a list, tuple, or any iterable object as multiple arguments to a function. For example, consider the following function definition and function call:

def greet(message, age, name="Bob"):
    print(f"Hello, {name}! {message} You are {age} years old.")

args = ["How are you?", 25]
greet(*args, name="Alice") # unpacking a list of non-keyword arguments

The function greet takes three parameters: name, message, and age. The function call passes two arguments: name= “Alice” and *args. The first is a keyword argument, specifying the parameter’s name. The second argument is a * operator, followed by a list of non-keyword arguments. The * operator unpacks the list and passes its elements as separate arguments to the function. Therefore, the function call is equivalent to:

greet("How are you?", 25, name="Alice")

This function call is valid, as it follows the order of arguments in a function call. The output of the function call is:

Hello, Alice! How are you? You are 25 years old.

FAQs

What is the difference between a keyword and non-keyword arguments in Python?

A keyword argument is an argument that is specified by using the name of the parameter and an equal sign, such as name= “Alice”. A non-keyword argument is an argument that is passed without specifying the name of the parameter, such as “Alice”. The passing of the Keyword arguments can be in any order, but non-keyword arguments must come before keyword arguments in a function call.

Why does Python not allow non-keyword arguments after keyword arguments in a function call?

Python does not allow non-keyword arguments after keyword arguments in a function call because it would create ambiguity and confusion. For example, if a function takes three parameters, and the function call passes a keyword argument followed by two non-keyword arguments, Python cannot determine which parameter the non-keyword arguments are matched with.

How can I pass multiple non-keyword arguments after a keyword argument in a function call?

You can pass multiple non-keyword arguments after a keyword argument in a function call by using the * operator to unpack a sequence of non-keyword arguments, such as a list, tuple, or any iterable object. The * operator allows you to pass the sequence elements as separate arguments to the function. For example, if you have a list of non-keyword arguments, you can write a *list after a keyword argument in a function call.

Conclusion

In this article, we have learned about the “SyntaxError non-keyword arg after keyword arg” in Python 3. We have seen what this error means, what causes it, and how to resolve it. We have also answered some frequently asked questions related to this error. We hope this article has helped you understand and avoid this error in your Python code.

References

  1. SynatxError
  2. Operator

Master day-to-day error handling by following PythonClear errors.

Leave a Comment