Generator

Applies to: python

A generator produces values one at a time with yield, computing each only when asked. It is lazy, so it uses little memory and can represent huge or infinite sequences.

def squares(n):
    for i in range(n):
        yield i * i      # produce lazily
for s in squares(3):     # 0, 1, 4
    ...

See also: list-comprehension, loop