Default Argument Values
Default argument values make certain function parameters optional by specifying a value to use if the caller does not provide one.
One Function, Several Greetings
Suppose a greeting function usually needs to use the word Hello, but sometimes should use Hi or Greetings. A caller should not have to provide a greeting word every time the usual choice is wanted. A default argument value lets the function use a specified value when the caller leaves that argument out, while still allowing the caller to supply a different value when needed.
A default argument value makes a function parameter optional: the caller may provide a value, but does not have to provide one.
Choosing the Value at Call Time
The decision is made when the function is called. If the caller supplies an argument for the parameter, Python uses that supplied argument. If the caller does not supply one, Python uses the value written in the function definition as the default.
What do you think happens?
A greeting parameter has the default value Hello. What value will the parameter hold when the caller supplies Hi?
Reveal answer
Answer: Hi
The default is used only when the caller does not provide an argument. When an argument is provided, Python uses the caller's value.
Writing the Parameter Syntax
The general pattern for a default argument is parameter_name = default_value. This pattern appears inside the parentheses of the function definition.
The equals sign connects one parameter name to the value Python should use when that parameter is not supplied by the caller. In a greeting function, a generated definition could use greeting = "Hello". The parameter is greeting, and its default value is Hello.
Tracing Two Calls
A Greeting with an Optional Word
A function has a greeting parameter whose default value is Hello. Determine the value held by greeting in two calls: one call leaves the argument out, and another supplies Greetings.
Call without an argument: The caller does not provide a value for greeting, so Python uses the default value Hello.
Call with an argument: The caller provides Greetings for greeting, so Python uses Greetings instead of the default.
The parameter holds Hello in the first call and Greetings in the second call.
The default does not permanently force the parameter to hold one value. It supplies a fallback for calls that omit the argument. A call that provides an argument selects the provided value instead.
Safe Default Values
Default values must be immutable. The source identifies strings, numbers, None, and tuples as examples of immutable values that can be used as defaults. Following this constraint helps avoid bugs caused by unintended modifications.
| Default category | Source guidance | Reason to remember it |
|---|---|---|
| Strings | Listed as immutable | Suitable as a default value |
| Numbers | Listed as immutable | Suitable as a default value |
| None | Listed as immutable | Suitable as a default value |
| Tuples | Listed as immutable | Suitable as a default value |
Mistakes with Optional Parameters
Assuming the default is used even when the caller supplies an argument.
Python uses the provided argument when one is given.
Fix:
Use the default only in your prediction when the caller leaves that parameter out.Putting the default value in the call instead of associating it with the parameter in the definition.
The source defines this pattern as syntax inside the function definition.
Fix:
Write the parameter name, equals sign, and default value in the function definition.Choosing a default without checking whether it is immutable.
The source warns that unintended modifications can cause bugs.
Fix:
Choose an immutable default such as a string, number, None, or tuple.Making every parameter mandatory even when a standard choice exists.
The function loses the convenience provided by an optional parameter.
Fix:
Give the parameter a default when a sensible fallback is available.
Apply the Selection Rule
A function has a parameter named greeting with the default value Hello. Predict the value of greeting in each situation: the caller provides no greeting; the caller provides Hi; the caller provides Greetings. Then explain why each result occurs.
Hints
- First check whether the caller supplied an argument.
- Use the default only when the argument is omitted.
- A supplied argument replaces the fallback for that call.
To predict a parameter's value, ask one question first: did the caller provide an argument for it? If yes, use the caller's value. If no, use the default written in the definition.
Design Checklist
- A default argument value makes a function parameter optional.
- Write a default using the pattern parameter_name = default_value inside the function definition.
- Python uses the caller's argument when one is supplied and the default when the argument is omitted.
- Choose immutable defaults; the source lists strings, numbers, None, and tuples as examples.
- Default values reduce the need to make callers provide a commonly used value repeatedly.
Key Takeaways
- Default argument values provide fallback values for omitted function arguments.
- The syntax is parameter_name = default_value in the function definition.
- A supplied argument takes priority over the default for that call.
- Immutable values such as strings, numbers, None, and tuples are identified as suitable default categories.
- Use defaults when a parameter has a sensible common choice that callers should not have to repeat.