Skip to content

lazy evaluation

Lazy evaluation is an evaluation strategy that delays computing an expression until its result is actually needed, then reuses that result instead of recomputing it. It is the opposite of eager, or strict, evaluation, which computes every expression as soon as it is bound, whether or not the program ever reads the value.

A lazy language wraps each pending expression in a thunk, a suspended computation that runs the first time its value is forced. Whether the forced value is cached separates the two main variants: call-by-need caches it after the first force, while call-by-name re-runs the thunk on every use.

A pending expression is wrapped in a thunk that the first use forces, then it splits into call by need, which caches and reuses, and call by name, which recomputes.
How a Thunk Defers and Then Forces a Value

Because work happens only on demand, a program can model an infinite sequence, such as all the prime numbers, and consume just the finite prefix it reads.

Lazy evaluation surfaces in several recurring forms:

  • Short-circuit operators, where Boolean operators like and and or skip their second operand once the result is settled.
  • Lazy data structures, such as streams and Python’s generators, that produce items on demand.
  • Control abstractions, where deferring an argument lets an ordinary function stand in for custom control flow.

The strategy trades a known cost for a deferred one. Skipping unused work saves time and memory, but unforced thunks can instead leak it, and the indeterminate order of deferred computations complicates side effects like input and output. Languages such as Haskell are lazy by default, while most others, including Python, expose laziness through opt-in tools like generators and iterators.

How to Use Generators and Yield in Python

Tutorial

How to Use Generators and yield in Python

In this step-by-step tutorial, you'll learn about generators and yielding in Python. You'll create generator functions and generator expressions using multiple Python yield statements. You'll also learn how to build data pipelines that take advantage of these Pythonic tools.

intermediate python

For additional information on related topics, take a look at the following resources:


By Martin Breuss • Updated July 16, 2026