Function Parameters
The central difference is the matching rule: positional arguments are matched by location, while keyword arguments are matched by an exact parameter name.
Why Position Can Mislead
A function call can provide values by position or by parameter name. Positional arguments are matched according to where they appear: the first value goes to the first parameter, the second value goes to the second parameter, and so on. This works, but the caller must remember the function's exact parameter order. Keyword arguments remove that burden by naming the parameter that should receive each value.
Imagine a function with several settings, such as a size, a color, and a layout choice. A positional call requires you to remember which setting belongs in each position. A keyword call makes each assignment visible, so a reader can identify the purpose of every supplied value directly from the call.
The central difference is the matching rule: positional arguments are matched by location, while keyword arguments are matched by an exact parameter name.
Positional Binding
With positional arguments, the interpreter reads the argument list from left to right. The value in the first position is assigned to the first parameter in the function definition, the value in the second position is assigned to the second parameter, and the pattern continues. Changing the order of the supplied values therefore changes which parameter receives each value.
Reading a Positional Call
A function expects three parameters in a particular order. A call supplies three values positionally.
- 1First match
The first supplied value is assigned to the first parameter.
- 2Second match
The second supplied value is assigned to the second parameter.
- 3Third match
The third supplied value is assigned to the third parameter.
- 4Order matters
If the caller rearranges the supplied values, the parameters receive different values, even though the same values were used.
A positional call is interpreted by location, so the caller must preserve the function's expected parameter order.
Named Argument Binding
A keyword argument supplies a value by naming the parameter that should receive it. The interpreter looks up that name and assigns the value directly to the matching parameter, ignoring the argument's position in the call.
Because the parameter name identifies the destination, keyword arguments can be written in any order. Reordering them does not change the mapping. This makes calls easier to read, particularly when a function has many parameters or when the purpose of a value is not obvious from its position.
Reordering Named Values
A function has parameters named size and color. A caller supplies values for both parameters using their names.
- 1Name the destination
The value assigned to size is matched directly to the size parameter.
- 2Name the other destination
The value assigned to color is matched directly to the color parameter.
- 3Change the written order
Writing the color assignment before the size assignment still sends each value to the same named parameter.
Keyword arguments can be reordered because their parameter names, rather than their positions, determine the mapping.
Mixed Argument Calls
A single function call may combine positional and keyword arguments. The required order is strict: every positional argument must appear before every keyword argument. The positional values are matched first by location, and the named values are then matched directly to their parameter names.
Use positional arguments when the order is clear and stable, then use keyword arguments for later values whose parameter names make the call easier to understand. This preserves the required ordering while making selected assignments explicit.
Defaults and Selective Overrides
Keyword arguments are especially useful when some parameters have default values. A caller can provide a keyword value for only the parameter that needs a different setting and rely on the function's defaults for the other parameters. This makes it possible to customize one part of a call without restating every available setting.
Changing One Setting
A function has several parameters, and some have default values. The caller wants to change only one of those settings.
- 1Identify the desired change
Choose the parameter whose default behavior should be different for this call.
- 2Supply its name
Pass the new value as a keyword argument using the exact parameter name.
- 3Leave other parameters out
Do not provide values for the remaining defaulted parameters when their defaults are acceptable.
The named parameter receives the explicit value, while the omitted parameters continue using their default values.
Mistakes with Names and Order
Common mistake Putting a positional argument after a keyword argument
What it looks likeA call begins with a named assignment and then supplies an unnamed value.
Why it failsAll positional arguments must come before all keyword arguments.
Do this insteadMove the positional value before the keyword assignments, or give that value a parameter name as well.
Common mistake Misspelling a parameter name
What it looks likeA keyword uses a name that resembles a parameter but does not exactly match it.
Why it failsThe interpreter requires an exact parameter-name match and does not guess which similar name was intended.
Do this insteadCheck the function's parameter names and use the exact spelling.
Common mistake Assuming keyword arguments still depend on their written order
What it looks likeA learner thinks reversing two correctly named keyword assignments changes their destinations.
Why it failsKeyword arguments are matched by name, not by position.
Do this insteadRead the name attached to each value; correctly named keyword arguments can be written in any order.
Common mistake Repeating values unnecessarily when defaults are suitable
What it looks likeA caller supplies every parameter even though only one setting needs to change.
Why it failsThis obscures the one intentional override and creates more values that must be remembered.
Do this insteadProvide the specific keyword override and allow the other parameters to use their defaults.
A keyword argument is only useful when its name exactly matches a parameter in the function definition. A near match is not treated as an alternative spelling; Python raises a TypeError instead of guessing.
Practice the Mapping
Consider a function with five parameters. Describe how its values are matched in each situation: first, when five values are supplied only by position; second, when the same values are supplied with parameter names in a different order; third, when two initial values are positional and the remaining selected values are keyword arguments; and fourth, when one defaulted parameter is overridden while the other defaulted parameters are omitted.
Hints
- For positional values, track the argument positions from left to right.
- For keyword values, track the exact parameter name instead of the written order.
- In a mixed call, place every positional value before every keyword value.
- An omitted defaulted parameter keeps its default value.
What do you think happens?
If two correctly named keyword arguments are exchanged in the written order of a function call, do they switch which parameters receive their values?
Reveal answer
No, because keyword arguments are matched by parameter name.
The interpreter ignores the keyword arguments' positions and assigns each value directly to the parameter with the exact supplied name.
Key Takeaways
- 1Positional arguments are assigned from left to right according to parameter order.
- 2Keyword arguments name their destination parameters, so their order in the call does not matter.
- 3When positional and keyword arguments are combined, all positional arguments must come first.
- 4A keyword name must exactly match a parameter name; otherwise Python raises a
TypeError. - 5Keyword arguments can override selected parameters while other parameters continue using their default values.
At a glance
- Positional calls depend on the exact order of parameters.
- Keyword calls assign values by exact parameter name and can be written in any order.
- Mixed calls are valid only when positional arguments appear before keyword arguments.
- Keyword arguments make selective overrides and default values easier to use and understand.