Language Syntax

Yield — Python syntax

Suspend a generator and produce a value to its consumer. Resumption continues after the yield; a sent value becomes the value of the yield expression. Calling a generator function does not immediately run its body. A bare yield produces None.

Formal Syntax & Structure

python syntaxpython
yieldyield expression

Parameters & Attributes

NameTypeDescription
expressionexpressionOptional value produced to caller.

Working Example

Example Snippetpython
def seq():    yield 1    yield 2g=seq()print((next(g),next(g)))
Python Reference