Skip to content

Yield and yield from

Like Pythons yield and yield from keywords.

Defining a generator function

Generator functions must use yield or yield from and return the Iterator type.

from pypp_python import Iterator


def yield_123() -> Iterator[int]:
    yield 1
    yield 2
    yield 3


def yield_over_list() -> Iterator[int]:
    for i in [1, 2, 3]:
        yield i


def yield_from_example() -> Iterator[int]:
    yield from yield_over_list()

Note: You cannot yield None in Py++

Usage

Generator functions can only be used in for loops

from pypp_python import mov


def pseudo_fn():
    a: list[int] = []
    for i in yield_123():
        y: int = i
        a.append(mov(y))