Keywords and Expressions
yield — Generator suspension and resumption
Suspend a generator and expose an item to its caller without discarding local state. A later next() resumes the generator with None as the value of the suspended yield expression; send(value) supplies a value instead. A newly created synchronous generator must be started with next() or send(None). The yield from form delegates iteration and supported send/throw operations; its expression result is the delegated iterator’s StopIteration.value. Async generators support yield but not yield from. Use explicit close or context-managed cleanup where required.
Formal Syntax & Structure
python syntaxpython
yield [expression_list]yield from iterablereceived = yield valueParameters & Attributes
| Name | Type | Description |
|---|---|---|
| expression_list | expression | Value or values exposed to the caller; omitted means None. |
| iterable | iterable | Required only in the yield from form. |
Return Value
object
The resumed yield expression evaluates to the sent value (or None); yield from evaluates to the delegated return value. The yielded item is delivered separately to the caller.
Working Example
Example Snippetpython
def readings(): adjustment = yield 10 yield 10 + adjustment
stream = readings()print(next(stream))print(stream.send(4))stream.close()