Keyword Arguments
When you use positional arguments, you must remember the exact order in which the function expects them. If a function takes five parameters, you need to count them correctly every time you call it. Keyword arguments eliminate this burden. You name each parameter explicitly, so the order no longer matters. This makes your code more readable and less error-prone, especially when a function has many parameters or when some of them have default values.
Why Names Beat Counting
A positional function call depends on order. The first supplied value goes to the first parameter, the second value goes to the second parameter, and so on. This means that calling a function with many parameters requires you to remember the function's exact parameter order. Keyword arguments remove that burden by naming the parameter that should receive each value. The call becomes easier to read because each value is connected directly to its purpose.
Use keyword arguments when a function has many parameters, when the purpose of a value is unclear from its position, or when you want a call to be self-documenting.
Defaults for Optional Parameters
A default argument value makes a parameter optional. In a function definition, write the parameter name, an equals sign, and the value to use when the caller does not provide an argument. The general pattern is parameter_name = default_value. For example, a greeting function might define greeting = "Hello". A caller who supplies no greeting receives the default, while a caller who supplies another greeting receives that supplied value.
Selecting a Greeting
Imagine a function with a message parameter and an optional greeting parameter whose default is "Hello". Determine the value of greeting in two calls: one call omits greeting, and the other supplies "Hi".
Call without greeting: No value is supplied for greeting, so Python uses the default value "Hello".
Call with greeting: The caller supplies "Hi", so Python uses that argument instead of the default.
Parameter state: The function body works with the resulting value in either case. It does not need to treat a supplied value and a default value as different kinds of parameter values.
The first call gives greeting the value "Hello". The second call gives greeting the value "Hi".
Safe Values for Defaults
A default should be a fixed, immutable value. Strings, numbers, tuples, and None are identified as safe default values because they cannot be changed after they are created. Lists and dictionaries are mutable, so they should not be used as defaults. If a mutable default is modified inside the function, that modification can persist across function calls and produce unexpected behavior.
| Default category | Examples | Guidance |
|---|---|---|
| Immutable | Strings, numbers, tuples, None | Safe choices for defaults |
| Mutable | Lists, dictionaries | Avoid as defaults because modifications can persist across calls |
Naming Arguments in Calls
A keyword argument assigns a value by naming the parameter in the function call. The keyword must exactly match a parameter name in the function definition. Python does not guess when the name is misspelled or does not exist; it raises a TypeError. Because matching uses names rather than positions, keyword arguments can appear in any order.
Same Values, Different Order
Consider a function with parameters item, quantity, and discount. Compare a call that names quantity first and discount second with a call that names discount first and quantity second.
First call: The keyword quantity assigns its value to quantity, and discount assigns its value to discount.
Second call: Although the keyword arguments appear in the opposite order, each value is still assigned by its exact parameter name.
Comparison: The order of the keyword arguments does not change the resulting parameter values.
Both calls produce the same parameter mapping because keyword arguments are matched by name rather than position.
Combining Positional and Keyword Forms
A function call may contain both positional and keyword arguments. The required order is strict: every positional argument must appear before every keyword argument. A positional argument after a keyword argument causes a syntax error. This rule keeps the two matching methods unambiguous: Python first handles the values identified by position, then handles the values identified by name.
The safe pattern is positional arguments first, followed by keyword arguments. Once the call has moved to keyword arguments, do not return to positional arguments.
Overriding Selected Defaults
Defaults are useful because they handle the common case without requiring every caller to provide every value. Keyword arguments make customization selective. A caller can name one optional parameter with a special value while omitting other optional parameters, allowing those omitted parameters to keep their defaults.
Changing One Optional Setting
Imagine an order-total function with an optional tax rate whose default is 0.08. Determine what happens when a caller supplies a different tax rate by name but omits the other optional settings.
Start with defaults: The function is designed with a standard tax rate for the common case.
Name the exception: The caller supplies a different value using the tax-rate parameter name.
Keep omitted defaults: Any other optional parameter that the caller does not name continues to receive its own default value.
The named tax-rate parameter uses the caller's value, while other omitted optional parameters use their defaults.
Mistakes in Argument Matching
Relying on position when the purpose of each value is unclear
The caller must remember the exact parameter order, and a misplaced value can be assigned to the wrong parameter.
Fix:
Use keyword arguments so each value is connected to its parameter name.Putting a positional argument after a keyword argument
Python requires all positional arguments to come before all keyword arguments, so the call raises a syntax error.
Fix:
Move the positional value before the keyword arguments, or name it as a keyword argument.Misspelling a keyword argument
Python does not guess or match similar names; an unknown keyword produces a TypeError.
Fix:
Use the exact parameter name from the function definition.Using a mutable value as a default
The modification can persist across function calls and lead to unexpected behavior.
Fix:
Use an immutable default such as a string, number, tuple, or None.
Check Your Understanding
A function is designed with three parameters: item, quantity, and discount. Quantity and discount have default values. Describe how you would call the function when you want to provide item and a custom discount but allow quantity to keep its default. Then describe why the positional part of the call must come before the keyword part.
Hints
- Supply the required item value first if it is being passed positionally.
- Name the discount parameter explicitly.
- Do not place an unnamed positional value after the named discount argument.
- The omitted quantity parameter receives its default.
What do you think happens?
A function has an optional greeting parameter with the default value "Hello". A caller supplies greeting="Greetings". Which value does greeting hold inside that call?
Reveal answer
Answer: "Greetings"
A supplied argument takes precedence over the default. The default is used only when the parameter is omitted.
What to Remember
- Positional arguments are matched by order; keyword arguments are matched by exact parameter name.
- Write parameter_name = default_value in a function definition to make a parameter optional.
- A supplied argument replaces its parameter's default, while an omitted optional parameter keeps its default.
- Use immutable defaults such as strings, numbers, tuples, or None; avoid mutable defaults such as lists and dictionaries.
- When combining argument styles, place every positional argument before every keyword argument.
Key Takeaways
- Keyword arguments name the parameters receiving values, so their order does not matter.
- Default values provide fallback values for omitted optional parameters.
- Each parameter independently receives either its supplied argument or its default.
- Keyword names must exactly match parameter names.
- Positional arguments must come before keyword arguments in a combined call.