Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A default parameter is a function parameter initialized with a fallback value within the function signature. This assignment makes the corresponding argument optional during function invocation. If the caller omits the argument, the Python interpreter automatically binds the parameter to the predefined default value. If an argument is explicitly passed, it overrides the default.

Syntax and Ordering

Default parameters are defined using the assignment operator (=) directly in the parameter list.
def configure_settings(mode, retries=3, timeout=5.0):
    pass
Ordering Rule: For positional and positional-or-keyword parameters, all parameters without default values (non-default parameters) must strictly precede parameters with default values. Violating this ordering raises a SyntaxError.

# INVALID: Non-default positional parameter follows default parameter
def invalid_func(a=1, b): 
    pass 

# Raises: SyntaxError: non-default argument follows default argument
However, this strict ordering rule does not apply to keyword-only parameters. Keyword-only parameters (defined after a bare * or *args) without default values can legally follow parameters that have default values.

# VALID: Keyword-only parameter 'b' has no default, but follows 'a' which does
def valid_func(a=1, *, b):
    pass

Evaluation Timing

A critical mechanical detail in Python is that default parameter values are evaluated exactly once, at the time the function definition (the def statement) is executed by the interpreter. They are not evaluated dynamically each time the function is invoked. The resulting evaluated objects are stored in two specific attributes on the function object:
  • __defaults__: A tuple storing the evaluated default values for positional and positional-or-keyword parameters.
  • __kwdefaults__: A dictionary storing the evaluated default values for keyword-only parameters.

The Mutable Default Anti-Pattern

Because default values are evaluated only once at definition time, using mutable objects (such as list, dict, or set) as default parameters causes the exact same object instance to be shared across all function calls that omit the argument. Modifications to the object persist between invocations.

# ANTI-PATTERN
def append_item(item, target_list=[]):
    target_list.append(item)
    return target_list

print(append_item(1))  # Output: [1]
print(append_item(2))  # Output: [1, 2] - The list instance is shared
To achieve dynamic default evaluation for mutable types, the standard idiom is to set the default parameter to the singleton None and instantiate the new mutable object within the function body.

# STANDARD IDIOM
def append_item(item, target_list=None):
    if target_list is None:
        target_list = []
    target_list.append(item)
    return target_list
Master Python with Deep Grasping Methodology!Learn More