> ## 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 Stacked Decorator

A stacked decorator in Python refers to the application of multiple decorators to a single function or method. This technique leverages function composition, where the output (the returned wrapper function) of one decorator becomes the input to the next, creating a nested chain of higher-order functions.

## Syntax

Decorators are stacked by placing multiple `@decorator_name` statements on consecutive lines directly above the target function definition.

```python theme={"dark"}
@decorator_alpha
@decorator_beta
@decorator_gamma
def target_function():
    pass
```

## Underlying Mechanics

When the Python interpreter evaluates a stacked decorator, it translates the syntactic sugar into standard function calls. The evaluation strictly follows mathematical function composition: $f(g(h(x)))$.

The syntax block above is functionally identical to the following reassignment:

```python theme={"dark"}
def target_function():
    pass

target_function = decorator_alpha(decorator_beta(decorator_gamma(target_function)))
```

## Application vs. Execution Order

Understanding stacked decorators requires distinguishing between **application order** (when the function is wrapped) and **execution order** (when the wrapped function is called).

1. **Application Order (Bottom-Up):**
   Decorators are applied from the innermost to the outermost. The decorator closest to the function definition (`@decorator_gamma`) receives the original function first. Its returned wrapper is then passed to `@decorator_beta`, and so on.
2. **Execution Order (Top-Down, then Bottom-Up):**
   When the decorated function is invoked, the execution flows through the wrappers from the outermost to the innermost. Code preceding the `func()` call executes top-down. Code following the `func()` call executes bottom-up as the call stack unwinds.

## Execution Flow Demonstration

The following code illustrates the call stack behavior of stacked decorators:

```python theme={"dark"}
def outer_decorator(func):
    def wrapper(*args, **kwargs):
        print("1. Entering outer_decorator")
        result = func(*args, **kwargs)
        print("5. Exiting outer_decorator")
        return result
    return wrapper

def inner_decorator(func):
    def wrapper(*args, **kwargs):
        print("2. Entering inner_decorator")
        result = func(*args, **kwargs)
        print("4. Exiting inner_decorator")
        return result
    return wrapper

@outer_decorator
@inner_decorator
def core_function():
    print("3. Executing core_function")


# Invoking the stacked function
core_function()
```

**Standard Output:**

```text theme={"dark"}
1. Entering outer_decorator
2. Entering inner_decorator
3. Executing core_function
4. Exiting inner_decorator
5. Exiting outer_decorator
```

## Metadata Preservation

Because stacked decorators create multiple layers of wrapper functions, the original function's introspection metadata (`__name__`, `__doc__`, `__annotations__`) is easily masked by the outermost wrapper.

To maintain accurate metadata through the entire stack, every decorator in the chain must utilize `functools.wraps`. If even one decorator in the stack omits it, the metadata chain is broken.

```python theme={"dark"}
from functools import wraps

def robust_decorator(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        return func(*args, **kwargs)
    return wrapper
```

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