Every Python developer hits this error eventually. You call a function, mix in some arguments with names and some without, and Python spits back: SyntaxError: Positional Argument Follows Keyword Argument. The message is clear, but the fix isn't always obvious, especially when you're new to the language. This guide breaks down exactly why Python enforces this rule, how to recognize it in your code, and the patterns that keep it from coming back.
Quick take:
- Positional arguments map by order; keyword arguments map by name
- Python requires all positional arguments before all keyword arguments
- Mixing both types is allowed when positional come first
- Default parameter values make arguments optional
- The rule prevents impossible ambiguity in parameter mapping
The Two Argument Types, Explained Simply
A positional argument relies on order. When you define a function with parameters num1 and num2, calling func(1, 2) assigns 1 to num1 and 2 to num2. Swap the order to func(2, 1) and those assignments flip. The position decides everything.
A keyword argument names its target explicitly. Calling func(num1=1, num2=2) assigns values by name instead. Func(num2=2, num1=1) produces the same result because each parameter gets its value directly. Order no longer matters.
Python allows mixing both types in a single call, but only if you follow the ordering rule. Positional arguments come first, keyword arguments come last. That's it.
Why Python Insists on This Order
Python needs unambiguous rules for mapping arguments to parameters. Consider this broken call:
Func(num1=1, 2)
The keyword argument num1=1 assigns 1 to num1. Then 2 appears. Does 2 map to num2? Does it try to override num1? Python can't answer, so it refuses to guess. The interpreter stops and throws the syntax error rather than risk running code that might do something unexpected.
By requiring positional arguments first, Python consumes them in sequence, then handles keyword arguments by name. No overlap, no confusion, no ambiguity. This rule is baked into the language because Python's flexibility with argument mixing would otherwise create genuine chaos.
The Fix: Reorder Your Arguments
The solution is straightforward: put positional arguments before keyword arguments.
This is wrong:
Func(num1=1, 2)
This is correct:
Func(1, num2=2)
Python reads left to right. It handles the positional 1, then sees the keyword argument num2=2, and everything maps cleanly.
Say you want to call a function that accepts a required name, an optional email, and an optional phone number. You might try:
Send_message("Alice", phone="555-1234")
This works fine. The positional argument "Alice" fills the required name parameter, and the keyword argument fills phone. Clean and readable.
Now say you only want to specify the phone. You can't skip the name positionally, but you can skip it with a keyword:
Send_message(name="Alice", phone="555-1234")
Both approaches are valid. The rule holds: positional first, keyword last.
Our Analysis: Why This Error Persists
This catches developers coming from languages with different argument semantics, or those learning Python's flexibility without understanding its constraints. The error message tells you exactly what's wrong, but it doesn't explain why the rule exists.
Once you see the ambiguity Python avoids, the rule becomes obvious. It also encourages better code habits. Mixing argument styles arbitrarily makes function calls harder to scan, especially with complex functions. Enforcing a consistent ordering improves readability across your codebase.
The rule also shapes how Python libraries are designed. Functions with many optional parameters typically expect keyword arguments, which makes calls self-documenting. You can read a function call six months later and immediately understand which optional values you're overriding. Positional-only arguments signal required parameters that must always be provided.
What to watch next: Python linters like pylint increasingly flag mixed argument styles as style violations, even when they're syntactically correct. Expect tooling to push developers toward clearer patterns. Some proposals floating around Python's development community suggest syntax improvements that could make these calls even more explicit, though nothing concrete has landed yet.
Bottom Line
The SyntaxError: Positional Argument Follows Keyword Argument is one of Python's friendlier errors. It tells you exactly what's wrong and exactly how to fix it. Remember one rule: positional first, keyword last. Default values make arguments optional; keyword arguments make optional arguments readable. Internalize this, and you'll never see the error again.
Key takeaways
- Always place positional arguments before keyword arguments in function calls
- Use keyword arguments for optional parameters to make code self-documenting
- Default values in function definitions let callers omit optional arguments entirely
- Python's rule exists to prevent impossible ambiguity in parameter mapping
- Modern linters may flag mixed argument styles even when they're valid
Frequently asked questions
Does this rule apply to lambda functions too?
Yes, the same argument ordering rules apply to lambdas. You can use both positional and keyword arguments depending on how the lambda is defined.
Can I use *args and **kwargs with this rule?
Yes. Positional arguments come before *args, which come before keyword arguments. **kwargs always come last if used.
Does this error occur in Python 2?
Python 2 handles this differently, typically showing warnings rather than strict syntax errors. Python 3 enforces the rule consistently.
What's the benefit of mixing positional and keyword arguments?
It lets you specify required parameters positionally while overriding optional ones by name. This keeps calls concise while maintaining clarity about which parameters you're setting.
Can I use keyword arguments for all parameters?
Yes, using only keyword arguments is valid and often preferable for functions with many parameters or optional arguments.