Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

generator.send

send supplies the result of a suspended yield expression and returns the next yielded value.

Version: Python 3.14; repository image, production runtime not attested

Syntax

yielded = gen.send(value)

send supplies the result of a suspended yield expression and returns the next yielded value.

generator.send

Prime a fresh generator with next or send(None). Any other initial value raises TypeError. A return ends the generator with its result in StopIteration.value.

The first resumption yields ready, sending 4 yields 8, and completion supplies done.

def channel():
    got=yield 1
    return got
g=channel()
try: g.send(4)
except TypeError: pass
else: raise AssertionError
assert next(g)==1
try: g.send(7)
except StopIteration as end: assert end.value==7
else: raise AssertionError

Examples

def channel():
    value = yield "ready"
    yield value * 2
    return "done"

g = channel()
print(g.send(None), g.send(4))
try:
    next(g)
except StopIteration as end:
    print(end.value)

Result

ready 8
done

The first resumption yields ready, sending 4 yields 8, and completion supplies done.

Sources