> ## 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 Yield From Statement

The `yield from` statement is a delegation mechanism in Python that allows a generator to yield all values from a sub-generator or iterable directly to the caller. Introduced in PEP 380, it establishes a transparent, bidirectional communication channel between the caller and the delegated sub-generator, automatically handling the propagation of values, exceptions, and return states.

## Syntax

```python theme={"dark"}
def delegating_generator():
    result = yield from iterable_or_subgenerator
```

## Core Mechanics

When a generator executes a `yield from` statement, it suspends its own execution and delegates control to the specified iterable or sub-generator. The delegating generator remains suspended until the sub-generator is exhausted.

### 1. Value Propagation

For simple iterables, `yield from` acts as syntactic sugar for a standard iteration loop. Every value produced by the sub-generator is passed directly to the caller of the delegating generator.

```python theme={"dark"}

# Naive equivalence for simple iterables
for item in iterable:
    yield item
```

### 2. Bidirectional Communication

Unlike a standard `for` loop, `yield from` binds the caller and the sub-generator together, allowing state to flow in both directions. If the caller uses generator methods to inject state, `yield from` routes them transparently:

* **`send(value)`**: Values sent to the delegating generator are passed directly into the sub-generator's `send()` method. If the sub-generator is a simple iterable (which lacks a `send` method), `send(None)` is permitted, but sending any other value raises an `AttributeError`.
* **`throw(type, value, traceback)`**: Exceptions thrown into the delegating generator are routed to the sub-generator's `throw()` method. If the delegated object is a simple iterable that lacks a `throw()` method, the thrown exception is simply raised directly in the delegating generator.
* **`close()`**: Calling `close()` on the delegating generator automatically invokes the `close()` method on the sub-generator, ensuring proper resource cleanup. If the delegated object lacks a `close()` method, the call is ignored.

### 3. Return Value Extraction

In Python, generators can return a value upon completion, which is attached to the `value` attribute of the raised `StopIteration` exception. The `yield from` expression automatically catches this `StopIteration`, extracts the `value`, and evaluates to that value within the delegating generator.

```python theme={"dark"}
def sub_generator():
    yield 1
    yield 2
    return "Execution finished"  # Attached to StopIteration.value

def delegating_generator():
    # 'yield from' yields 1 and 2 to the caller.
    # Once exhausted, it evaluates to "Execution finished".
    return_val = yield from sub_generator()
    yield f"Sub-generator returned: {return_val}"


# Execution:

# list(delegating_generator()) -> [1, 2, 'Sub-generator returned: Execution finished']
```

## Exception Handling Behavior

The `yield from` statement enforces strict exception handling rules between the caller, the delegating generator, and the sub-generator:

1. If the sub-generator raises a `StopIteration`, `yield from` consumes it and returns the exception's value.
2. If the sub-generator raises any other unhandled exception, the exception propagates up to the delegating generator, terminating the `yield from` expression.
3. If a `GeneratorExit` exception is raised in the delegating generator (typically via `close()`), it is passed down to the sub-generator. If the sub-generator fails to exit (e.g., it catches `GeneratorExit` and yields another value), a `RuntimeError` is raised in the delegating generator.

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