Strict and non-strict evaluation
Functional programming's efficiency stems, in part, from being able to defer a computation until it's required. The idea of lazy or non-strict evaluation is very helpful. To an extent, Python offers this feature.
In Python, the logical expression operators and, or, and if-then-else are all non-strict. We sometimes call them short-circuit operators because they don't need to evaluate all arguments to determine the resulting value.
The following command snippet shows the and operator's non-strict feature:
>>> 0 and print("right") 0 >>> True and print("right") right
When we execute the first of the preceding command snippet, the left-hand side of the and operator is equivalent to False; the right-hand side is not evaluated. In the second example, when the left-hand side is equivalent to True, the right-hand side is evaluated.
Other parts of Python are strict. Outside the logical operators, an expression is evaluated eagerly from left to right. A sequence of statement lines is also evaluated strictly in order. Literal lists and tuples require eager evaluation.
When a class is created, the method functions are defined in a strict order. In the case of a class definition, the method functions are collected into a dictionary (by default) and order is not maintained after they're created. If we provide two methods with the same name, the second one is retained because of the strict evaluation order.
Python's generator expressions and generator functions, however, are lazy. These expressions don't create all possible results immediately. It's difficult to see this without explicitly logging the details of a calculation. Here is an example of the version of the range() function that has the side effect of showing the numbers it creates:
def numbers():
for i in range(1024):
print(f"= {i}")
yield i
To provide some debugging hints, this function prints each value as the value is yielded. If this function were eager, it would create all 1,024 numbers. Since it's lazy, it only creates numbers as requested.
We can use this noisy numbers() function in a way that will show lazy evaluation. We'll write a function that evaluates some, but not all, of the values from this iterator:
def sum_to(n: int) -> int:
sum: int = 0
for i in numbers():
if i == n: break
sum += i
return sum
The sum_to() function has type hints to show that it should accept an integer value for the n parameter and return an integer result. The sum variable also includes Python 3 syntax, : int, a hint that it should be considered to be an integer. This function will not evaluate the entire result of the numbers() function. It will break after only consuming a few values from the numbers() function. We can see this consumption of values in the following log:
>>> sum_to(5) = 0 = 1 = 2 = 3 = 4 = 5 10
As we'll see later, Python generator functions have some properties that make them a little awkward for simple functional programming. Specifically, a generator can only be used once in Python. We have to be cautious with how we use the lazy Python generator expressions.