2.3 Strict and non-strict evaluation
Functional programming’s efficiency stems, in part, from being able to defer a computation until it’s required. There are two similar concepts for avoiding computation. These are:
Strictness: Python operators are generally strict and evaluate all sub-expressions from left to right. This means an expression like
f(a)+f(b)+f(c)
is evaluated as if it was(f(a)+f(b))+f(c)
. An optimizing compiler might avoid strict ordering to improve performance. Python doesn’t optimize and code is mostly strict. We’ll look at cases where Python is not strict below.Eagerness and laziness: Python operators are generally eager and evaluate all sub-expressions to compute the final answer. This means
(3-3)
*
f(d)
is fully evaluated even though the first part of the multiplication—the(3-3)
sub-expression—is always zero, meaning the result is always zero, no matter what value is computed by the expressionf(d)
. Generator...