Default Argument Values
A default argument value makes a function parameter optional: the caller may provide a value, but does not have to provide one.
Try it: Binding Arguments to Parameters
How Python matches a call's arguments to a function's parameters — positionals left to right, extras into *args, keywords by name, defaults for the rest, unknown names into **kwargs — and the exact SyntaxError or TypeError when a call doesn't fit, including the shared mutable default.
How it works
- The parser checks the call first: a positional after a keyword, or a repeated keyword, is a SyntaxError before anything runs.
- At the call site, *list spreads into separate positional arguments and **dict into keyword arguments.
- Positional arguments fill the parameters left to right; leftovers go into the *args tuple.
- Each keyword matches a parameter by exact name (a second value is 'got multiple values'); unknown names go into **kwargs or raise 'unexpected keyword argument'.
- Then: too many positionals, or a required parameter still empty, is a TypeError; otherwise defaults fill the rest.
- Defaults are evaluated once, at def time: a list default is the same object in every call.
Default run (13 steps): def greet(…) runs once: it creates the function and evaluates its default values right now. Then the call greet('Ann', 'Hi', *[7, 8], sep='-', mood='happy') starts. … Done. Printed: {'name': 'Ann', 'greeting': 'Hi', 'sep': '-', 'args': (7, 8), 'kwargs': {'mood': 'happy'}}.
Simplified: Five hand-written signatures with no positional-only (/) parameters; argument values are small literals (ints, short strs, True/False/None, flat lists). Error messages are CPython 3.12's (3.13+ may add a 'Did you mean' hint).
Loading the simulation…
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
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.
- 1Call without an argument
The caller does not provide a value for greeting, so Python uses the default value Hello.
- 2Call 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 |
The source identifies these immutable values as examples of safe default-value categories.
Mistakes with Optional Parameters
Common mistake Assuming the default is used even when the caller supplies an argument.
What it looks likeA greeting parameter has the default Hello, but the caller supplies Hi.
Why it failsPython uses the provided argument when one is given.
Do this insteadUse the default only in your prediction when the caller leaves that parameter out.
Common mistake Putting the default value in the call instead of associating it with the parameter in the definition.
What it looks likeTreating parameter_name = default_value as call-time input.Why it failsThe source defines this pattern as syntax inside the function definition.
Do this insteadWrite the parameter name, equals sign, and default value in the function definition.
Common mistake Choosing a default without checking whether it is immutable.
What it looks likeSelecting a value that can be modified as a default.
Why it failsThe source warns that unintended modifications can cause bugs.
Do this insteadChoose an immutable default such as a string, number, None, or tuple.
Common mistake Making every parameter mandatory even when a standard choice exists.
What it looks likeRequiring every caller to provide Hello when most greetings use that word.
Why it failsThe function loses the convenience provided by an optional parameter.
Do this insteadGive 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
- 1A default argument value makes a function parameter optional.
- 2Write a default using the pattern
parameter_name=default_valueinside the function definition. - 3Python uses the caller's argument when one is supplied and the default when the argument is omitted.
- 4Choose immutable defaults; the source lists strings, numbers, None, and tuples as examples.
- 5Default 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_valuein 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.