Concepts / Default Argument Values

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.

  • Programming
Interactive lab

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

  1. The parser checks the call first: a positional after a keyword, or a repeated keyword, is a SyntaxError before anything runs.
  2. At the call site, *list spreads into separate positional arguments and **dict into keyword arguments.
  3. Positional arguments fill the parameters left to right; leftovers go into the *args tuple.
  4. 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'.
  5. Then: too many positionals, or a required parameter still empty, is a TypeError; otherwise defaults fill the rest.
  6. 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).

Educational simulation

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.

Key point

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

Function callArgument providedCaller valueDefault valuecheck parameteryesno
Which value does the parameter hold when the caller supplies an argument, and which value does it hold when the caller leaves the argument out?

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?

  • Hello

  • Hi

  • Both values at the same time

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

parameter_nameparameter=assignment operatordefault_valuefallback value
Where does the default value appear in a function definition, and how is it associated with its parameter?
Definition

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

Worked example

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.

  1. 1
    Call without an argument

    The caller does not provide a value for greeting, so Python uses the default value Hello.

  2. 2
    Call with an argument

    The caller provides Greetings for greeting, so Python uses Greetings instead of the default.

Result

The parameter holds Hello in the first call and Greetings in the second call.

greetingHellogreetingGreetingscaller supplies Greetings
How does the selected value change when the caller moves from omitting the argument to supplying a caller value?

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 categorySource guidanceReason to remember it
StringsListed as immutableSuitable as a default value
NumbersListed as immutableSuitable as a default value
NoneListed as immutableSuitable as a default value
TuplesListed as immutableSuitable 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 like

    A greeting parameter has the default Hello, but the caller supplies Hi.

    Why it fails

    Python uses the provided argument when one is given.

    Do this instead

    Use 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 like
    Treating parameter_name = default_value as call-time input.
    Why it fails

    The source defines this pattern as syntax inside the function definition.

    Do this instead

    Write 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 like

    Selecting a value that can be modified as a default.

    Why it fails

    The source warns that unintended modifications can cause bugs.

    Do this instead

    Choose 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 like

    Requiring every caller to provide Hello when most greetings use that word.

    Why it fails

    The function loses the convenience provided by an optional parameter.

    Do this instead

    Give the parameter a default when a sensible fallback is available.

Apply the Selection Rule

EASY

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.
Key point

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

  1. 1
    A default argument value makes a function parameter optional.
  2. 2
    Write a default using the pattern parameter_name = default_value inside the function definition.
  3. 3
    Python uses the caller's argument when one is supplied and the default when the argument is omitted.
  4. 4
    Choose immutable defaults; the source lists strings, numbers, None, and tuples as examples.
  5. 5
    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.

Related concepts

More concepts starting with D