Function Design Best Practices
Default values make selected parameters optional. The caller can accept the function's standard behavior without supplying that argument, or provide a different value when customization is needed.
A Useful Fallback
A function often has one behavior that is useful in most situations, while still needing to support special cases. For example, a greeting function might normally use the word Hello, but sometimes a caller may want Hi or Greetings. A default argument value lets the function provide the common choice automatically while allowing the caller to replace it when necessary.
A default argument value is a value assigned to a function parameter that Python uses when the caller does not provide an argument for that parameter.
Default values make selected parameters optional. The caller can accept the function's standard behavior without supplying that argument, or provide a different value when customization is needed.
Tracing the Argument Choice
When Python encounters a function call, it checks the arguments supplied by the caller. If the caller provides a value for a parameter, that value is used. If the caller omits an argument for a parameter with a default, Python uses the default instead. After the parameter is bound to a value, the function body works with that value in the same way, regardless of whether it came from the caller or from the default.
What do you think happens?
A greeting function has a greeting parameter whose default is Hello. What value will greeting hold when the caller omits that argument?
Reveal answer
Hello
The default value is used when the caller does not provide an argument for that parameter.
Writing the Default
The general pattern for a default parameter is parameter_name = default_value. This pattern appears inside the parentheses of the function definition. The parameter name identifies which parameter receives the value, the equals sign connects the parameter to its fallback, and the value on the right is used when the caller omits that argument.
Choosing a Greeting
Design a greeting function whose greeting parameter uses Hello by default but accepts a different greeting when the caller provides one.
- 1Choose the common case
Use Hello as the default because it is the greeting wanted in the usual case.
- 2Attach the fallback
Write the parameter and its default in the form greeting = Hello inside the function definition.
- 3Trace an omitted argument
If the caller does not supply greeting, the parameter receives Hello.
- 4Trace a supplied argument
If the caller supplies Hi, the parameter receives Hi instead of the default.
One function supports both the common greeting and customized greetings without requiring the caller to provide a greeting every time.
A default does not force the parameter to keep one value. It supplies a value only when that parameter is omitted from the call.
Independent Parameters
Python makes the argument-or-default decision independently for each parameter. A function can therefore receive a caller-provided value for one parameter while using the default for another. Once each parameter has been bound, the function body simply works with the resulting values.
Calculating an Order Total
An order-total function uses a tax rate of 0.08 by default, but some callers need to provide a different tax rate.
- 1Default case
When the caller supplies an order but omits the tax rate, the tax-rate parameter receives 0.08.
- 2Special case
When the caller supplies a different tax rate, that supplied value replaces 0.08 for that call.
- 3Design result
The function handles the common tax situation conveniently while remaining flexible for callers with a different rate.
The default tax rate provides sensible behavior for typical calls, and an explicit argument supports specialized calls.
This pattern is useful when a function has a sensible value for most cases but should still allow customization. The default reduces unnecessary input for ordinary use, while the optional argument keeps the function useful in less common situations.
Safe Default Values
A default should be a fixed, immutable value. Immutable values cannot be changed after they are created. The source identifies strings, numbers, tuples, and None as safe default values.
| Default category | Examples | Design guidance |
|---|---|---|
| Immutable | Strings, numbers, tuples, None | Safe choices for defaults |
| Mutable | Lists, dictionaries | Avoid because modifications can persist across calls |
The source recommends immutable values as defaults and warns against mutable values.
Common Design Mistakes
Common mistake Treating a default as a required argument
What it looks likeAssuming the caller must always provide the parameter even though the function definition gives it a default.
Why it failsThe purpose of a default is to make that parameter optional when the caller does not provide an argument.
Do this insteadLet the function use its fallback for the common case, and provide an argument only when a different value is needed.
Common mistake Expecting the default to override a supplied argument
What it looks likeA caller supplies a different greeting but the function is expected to use the standard greeting anyway.
Why it failsPython uses the provided argument when one exists.
Do this insteadRemember that the default is used only when the argument is omitted.
Common mistake Using a mutable value as a default
What it looks likeChoosing a list or dictionary as a default and modifying it inside the function.
Why it failsThe modification can persist across function calls and produce unexpected behavior.
Do this insteadUse an immutable value such as a string, number, tuple, or None.
Common mistake Assuming every parameter receives its value from the same source
What it looks likeExpecting all parameters to use defaults because one argument was omitted.
Why it failsPython decides independently for each parameter.
Do this insteadTrace each parameter separately: a supplied argument wins for that parameter, while an omitted argument selects its default.
A function has a message parameter with the default value Hello and a rate parameter with the default value 0.08. For each call, identify the value held by each parameter: a call that supplies neither argument, a call that supplies only message as Hi, and a call that supplies only rate as 0.05.
Hints
- For each parameter, ask whether the caller supplied an argument.
- Use the supplied value when present; otherwise use that parameter's default.
- Do not let the choice for one parameter determine the choice for the other.
- 1Default argument values provide fallback behavior for omitted arguments. Write them as
parameter_name=default_valuein the function definition. A supplied argument replaces the default for that call, and each parameter is evaluated independently. Prefer immutable defaults such as strings, numbers, tuples, and None; avoid mutable defaults such as lists and dictionaries because modifications can persist across calls.
Key Takeaways
- Default argument values make selected function parameters optional.
- The syntax is
parameter_name=default_valueinside the function definition. - Python uses a supplied argument when present and the default when the argument is omitted.
- The choice is made independently for each parameter.
- Use immutable defaults such as strings, numbers, tuples, and None; avoid mutable lists and dictionaries.