The most important kind of compound expression is a call expression, which applies a function to some arguments. Recall from algebra that the mathematical notion of a function is a mapping from some input arguments to an output value. For instance, the max function maps its inputs to a single output, which is the largest of the inputs. The way in which Python expresses function application is the same as in conventional mathematics.
>>> max(7.5, 9.5)
9.5
最重要的一种复合表达式是调用表达式,它将一个函数应用于一些参数。回顾一下代数,函数的数学概念是从一些输入参数到输出值的映射。例如,max 函数把它的输入映射到一个单一的输出,也就是输入中最大的那个。Python 表达函数应用的方式与传统数学中相同。
This call expression has subexpressions: the operator is an expression that precedes parentheses, which enclose a comma-delimited list of operand expressions.
这个调用表达式有子表达式:在括号前,operator 是一个表达式,括号内有一个以逗号分隔的operand 表达式列表。
The operator specifies a function. When this call expression is evaluated, we say that the function max is called with arguments 7.5 and 9.5, and returns a value of 9.5.
The order of the arguments in a call expression matters. For instance, the function pow raises its first argument to the power of its second argument.
这个operator指定了一个函数。当这个调用表达式被评估时,我们说函数max被调用,参数为7.5和9.5,返回值为9.5。 调用表达式中参数的顺序很重要。例如,函数pow将其第一个参数提高到第二个参数的幂。
>>> pow(100, 2)
10000
>>> pow(2, 100)
1267650600228229401496703205376
Function notation has three principal advantages over the mathematical convention of infix notation. First, functions may take an arbitrary number of arguments:
与数学惯例的infix记号相比,函数记号有三个主要优点。首先,函数可以接受任意数量的参数。
>>> max(1, -2, 3, -4)
3
No ambiguity can arise, because the function name always precedes its arguments.
Second, function notation extends in a straightforward way to nested expressions, where the elements are themselves compound expressions. In nested call expressions, unlike compound infix expressions, the structure of the nesting is entirely explicit in the parentheses.
不会产生歧义,因为函数名总是在其参数之前 第二,函数符号以一种直接的方式扩展到嵌套表达式,其中的元素本身就是复合表达式。在嵌套调用表达式中,与复合infix表达式不同,嵌套的结构在括号中是完全明确的。
>>> max(min(1, -2), min(pow(3, 5), -4))
-2
There is no limit (in principle) to the depth of such nesting and to the overall complexity of the expressions that the Python interpreter can evaluate. However, humans quickly get confused by multi-level nesting. An important role for you as a programmer is to structure expressions so that they remain interpretable by yourself, your programming partners, and other people who may read your expressions in the future.
这种嵌套的深度和Python解释器可以评估的表达式的整体复杂性,(原则上)是没有限制。然而,人类很快就会被多级嵌套所迷惑。作为一个程序员,你的一个重要角色是构建表达式,使表达式对你自己、你的编程伙伴以及将来可能阅读你的表达式的其他人保持可解释的。
Third, mathematical notation has a great variety of forms: multiplication appears between terms, exponents appear as superscripts, division as a horizontal bar, and a square root as a roof with slanted siding. Some of this notation is very hard to type! However, all of this complexity can be unified via the notation of call expressions. While Python supports common mathematical operators using infix notation (like + and -), any operator can be expressed as a function with a name.
第三,数学符号的形式多种多样:乘法出现在项之间,指数作为上标出现,除法作为横杠,平方根作为有斜面的屋顶。其中有些符号是很难打出来的!然而,所有这些复杂性都可以通过调用表达式的符号来统一。虽然Python支持使用infix符号的常见数学运算符 (像+和-),但任何运算符都可以表示为一个有名字的函数。