官术网_书友最值得收藏!

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.

The older Python 2 range() function was eager and created an actual list object with all of the requested numbers. The Python 3 range() object is lazy, and will not create a large data structure.

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.

主站蜘蛛池模板: 兴海县| 屏东县| 大名县| 宁远县| 新泰市| 兰州市| 巧家县| 弥勒县| 临江市| 阳原县| 清远市| 双辽市| 文山县| 二连浩特市| 霍林郭勒市| 收藏| 剑川县| 光泽县| 马边| 新余市| 荣成市| 潜江市| 徐水县| 叶城县| 九龙城区| 太和县| 封开县| 汕头市| 自治县| 芦山县| 台北县| 元阳县| 通江县| 大洼县| 壶关县| 西盟| 瑞金市| 五寨县| 嵊泗县| 崇礼县| 同德县|