CKTIS / Python
generator.send
send fornisce il risultato dell’espressione yield sospesa e restituisce il prossimo valore prodotto.
Versione: Python 3.14; repository image, production runtime not attested
Sintassi
yielded = gen.send(value)send fornisce il risultato dell’espressione yield sospesa e restituisce il prossimo valore prodotto.
generator.send
Un generatore nuovo si avvia con next o send(None), un altro valore iniziale solleva TypeError. return lo termina con il risultato in StopIteration.value.
La prima ripresa produce ready, inviare 4 dà 8 e alla fine si ottiene 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
Esempi
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)
Risultato
ready 8 done
La prima ripresa produce ready, inviare 4 dà 8 e alla fine si ottiene done.