> ## 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.

# Python Default Parameter

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.

```python theme={"dark"}
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`.

```python theme={"dark"}

# 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.

```python theme={"dark"}

# 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.

```python theme={"dark"}

# 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.

```python theme={"dark"}

# STANDARD IDIOM
def append_item(item, target_list=None):
    if target_list is None:
        target_list = []
    target_list.append(item)
    return target_list
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Python Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
