> ## 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 Multiple Context Managers

Python allows the execution of multiple context managers within a single `with` statement. This mechanism binds multiple runtime contexts to a single block of code, ensuring that the setup (`__enter__`) and teardown (`__exit__`) protocols for all specified objects are managed collectively without requiring deep indentation.

## Syntax

**Standard Comma-Separated (Python 3.1+)**
Multiple context managers are separated by commas.

```python theme={"dark"}
with context_manager_A() as a, context_manager_B() as b:
    # Execution block
```

**Parenthesized Continuation (Python 3.10+)**
Python 3.10 introduced support for enclosing multiple context managers in parentheses, allowing them to be formatted across multiple lines for PEP 8 compliance.

```python theme={"dark"}
with (
    context_manager_A() as a,
    context_manager_B() as b,
    context_manager_C() as c
):
    # Execution block
```

Semantically, declaring multiple context managers in a single `with` statement is strictly equivalent to nesting them:

```python theme={"dark"}
with context_manager_A() as a:
    with context_manager_B() as b:
        with context_manager_C() as c:
            # Execution block
```

## Execution Mechanics

The evaluation and execution of multiple context managers follow a strict directional order:

1. **Initialization and `__enter__` (Left-to-Right):**
   The context managers are evaluated from left to right. The `__enter__()` method of the leftmost context manager is invoked first. If an `as` clause is present, the return value is bound to the target variable. This process repeats sequentially for each subsequent context manager to the right.
2. **`__exit__` (Right-to-Left / LIFO):**
   Upon exiting the `with` block, the `__exit__()` methods are invoked in Last-In, First-Out (LIFO) order. The rightmost (innermost) context manager's teardown logic executes first, propagating backwards to the leftmost (outermost) context manager.

## Exception Propagation

Handling exceptions across multiple context managers relies on the LIFO teardown sequence and the boolean return value of the `__exit__` methods:

* **Exceptions during `__enter__`:** If an exception is raised during the `__enter__` phase of the *Nth* context manager, the `__exit__` methods of the *1st* through *(N-1)th* context managers are immediately invoked in reverse order. The *Nth* context manager's `__exit__` is not called.
* **Exceptions within the block:** If an exception occurs inside the `with` block, the exception type, value, and traceback are passed to the `__exit__` method of the rightmost context manager.
  * If the rightmost `__exit__` returns `False` (or `None`), the exception propagates to the next context manager to the left.
  * If the rightmost `__exit__` returns `True`, the exception is suppressed. The remaining context managers to the left will still have their `__exit__` methods called, but they will receive `None` for the exception arguments, as if the block exited normally.

## Dynamic Multiple Context Managers

When the exact number of context managers is unknown at parsing time, Python provides `contextlib.ExitStack`. This class manages a dynamic stack of context managers, preserving the exact left-to-right setup and right-to-left teardown semantics of a static multiple-context `with` statement.

```python theme={"dark"}
from contextlib import ExitStack

managers = [context_manager_A(), context_manager_B(), context_manager_C()]

with ExitStack() as stack:
    # enter_context() pushes the manager onto the stack and returns its __enter__ value
    results = [stack.enter_context(mgr) for mgr in managers]
    
    # Execution block
```

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