Throughout this text, we will distinguish between two types of functions.
Pure functions. Functions have some input (their arguments) and return some output (the result of applying them). The built-in function
在这篇文章中,我们将区分两种类型的函数。 纯函数。函数有一些输入(它们的参数)并返回一些输出(应用它们的结果)。内置函数
abs(-2)
2
can be depicted as a small machine that takes input and produces output. 可以被描述为一台接受输入并产生输出的小型机器。
The function abs is pure. Pure functions have the property that applying them has no effects beyond returning a value. Moreover, a pure function must always return the same value when called twice with the same arguments.
函数abs是纯函数。纯函数具有这样的特性:调用它们除了返回一个值之外,没有任何影响。此外,纯函数在用相同的参数调用两次时必须总是返回相同的值。
Non-pure functions. In addition to returning a value, applying a non-pure function can generate side effects, which make some change to the state of the interpreter or computer. A common side effect is to generate additional output beyond the return value, using the print function.
非纯函数,除了返回一个值,调用一个非纯函数可以生成副作用,这会改变解释器或者计算机的一些状态。一个常见的副作用是使用print函数,在返回值之外产生额外的输出。
print(1, 2, 3)
1 2 3
While print and abs may appear to be similar in these examples, they work in fundamentally different ways. The value that print returns is always None, a special Python value that represents nothing. The interactive Python interpreter does not automatically print the value None. In the case of print, the function itself is printing output as a side effect of being called.
虽然 print 和 abs 在这些例子中看起来很相似,但它们的工作方式根本上是不同的。print 返回的值总是 None,一个特殊的 Python 值,代表什么都没有。Python交互式解释器不会自动打印 None 这个值。在 print 的情况下,函数本身是打印输出的,这是被调用的一个副作用
A nested expression of calls to print highlights the non-pure character of the function.
调用Print的嵌套表达式突出了函数的非纯特性
print(print(1), print(2))
1 2
None None
If you find this output to be unexpected, draw an expression tree to clarify why evaluating this expression produces this peculiar output.
如果你发现这个输出是出乎意料的,画出表达式树来弄清为什么这个表达式的求值会产生奇怪的输出
Be careful with print! The fact that it returns None means that it should not be the expression in an assignment statement.
小心print ! 实际上这个返回值为None,意味着不应该在赋值语句中用作表达式。
two = print(2)
2
>>> print(two)
None
Pure functions are restricted in that they cannot have side effects or change behavior over time. Imposing these restrictions yields substantial benefits. First, pure functions can be composed more reliably into compound call expressions. We can see in the non-pure function example above that print does not return a useful result when used in an operand expression. On the other hand, we have seen that functions such as max, pow and sqrt can be used effectively in nested expressions.
纯函数的局限性在于它们不能有副作用或随时间改变行为。实施这些限制会带来巨大的好处。首先,纯函数可以更可靠地组合成复合调用表达式。我们可以在上面的非纯函数示例中看到,print在操作数表达式中使用时没有返回有用的结果。另一方面,我们已经看到max、pow和sqrt等函数可以有效地用在嵌套表达式中。
Second, pure functions tend to be simpler to test. A list of arguments will always lead to the same return value, which can be compared to the expected return value. Testing is discussed in more detail later in this chapter.
第二,纯函数往往更容易测试。一列参数将总是导致相同的返回值,可以与预期的返回值进行比较。本章后面将更详细地讨论测试。
Third, Chapter 4 will illustrate that pure functions are essential for writing concurrent programs, in which multiple call expressions may be evaluated simultaneously.
第三,第四章将说明纯函数对于编写并发程序是必不可少的,在并发程序中可以同时计算多个调用表达式。
By contrast, Chapter 2 investigates a range of non-pure functions and describes their uses.
相比之下,第2章研究了一系列非纯函数并描述了它们的用途。
For these reasons, we concentrate heavily on creating and using pure functions in the remainder of this chapter. The print function is only used so that we can see the intermediate results of computations.
由于这些原因,在本章的剩余部分,我们主要集中在创建和使用纯函数上。使用print函数只是为了让我们可以看到计算的中间结果。