5.3.4 调用 Calls

A call calls a callable object (e.g., a function) with a possibly empty series of arguments:

一个调用就是以一系列参数(可能为空)调用一个可调用对象(例如, 函数):

call  ::=  primary "(" [argument_list [","]] ")"
argument_list  ::=  positional_arguments ["," keyword_arguments]
                         ["," "*" expression]
                         ["," "**" expression]
    | keyword_arguments ["," "*" expression]
                        ["," "**" expression]
    | "*" expression ["," "**" expression]
    | "**" expression
positional_arguments  ::=  expression ("," expression)*
keyword_arguments  ::=  keyword_item ("," keyword_item)*
keyword_item  ::=  identifier "=" expression
Download entire grammar as text.

A trailing comma may be present after an argument list but does not affect the semantics.

在参数表后面可以出现一个逗号, 但它在语义上是没有任何作用的.

The primary must evaluate to a callable object (user-defined functions, built-in functions, methods of built-in objects, class objects, methods of class instances, and certain class instances themselves are callable; extensions may define additional callable object types). All argument expressions are evaluated before the call is attempted. Please refer to section 7.5 for the syntax of formal parameter lists.

首先的工作是导出一个可调用对象(用户自定义函数, 内建函数, 内建方法对象, 类定义, 类实例方法, 某些类实例自身就是可调用的, 扩展模块可能定义了自己的可调用对象)所有的参数表达都在试图调用之前被计算关于形参表的句法参见7.5.

If keyword arguments are present, they are first converted to positional arguments, as follows. First, a list of unfilled slots is created for the formal parameters. If there are N positional arguments, they are placed in the first N slots. Next, for each keyword argument, the identifier is used to determine the corresponding slot (if the identifier is the same as the first formal parameter name, the first slot is used, and so on). If the slot is already filled, a TypeError exception is raised. Otherwise, the value of the argument is placed in the slot, filling it (even if the expression is None, it fills the slot). When all arguments have been processed, the slots that are still unfilled are filled with the corresponding default value from the function definition. (Default values are calculated, once, when the function is defined; thus, a mutable object such as a list or dictionary used as default value will be shared by all calls that don't specify an argument value for the corresponding slot; this should usually be avoided.) If there are any unfilled slots for which no default value is specified, a TypeError exception is raised. Otherwise, the list of filled slots is used as the argument list for the call.

如果给出了关键字参数, 它们首先被转换为位置参数.具体如下:第一步,根据形参表创建一串空闲槽, 如果有N个位置参数, 它们就被放在前N个槽中. 然后, 对于每个关键字参数, 它的标识符用于检测其对应的槽(如果其标识符与第一个形参数名相同, 它就占用第一个槽,以此类推)如果发现某个槽已经被占用, 则引发TypeError异常.否则将参数的值(即使为None)放进槽中. 当所有关键字参数处理完成后, 所有未填充的槽用在函数定义中的相应的默认值填充. (默认值是由函数定义时计算出来的, 所以, 像列表和字典这样的可变类型对象作默认值时, 它们会被那些没有相应槽指定参数值的调用所共享, 通常要避免这样做). 如果仍有未填充默认的槽位, 就会引发一个TypeError异常.否则, 所有被填充的槽当作调用的参数表.

If there are more positional arguments than there are formal parameter slots, a TypeError exception is raised, unless a formal parameter using the syntax "*identifier" is present; in this case, that formal parameter receives a tuple containing the excess positional arguments (or an empty tuple if there were no excess positional arguments).

如果位置参数的个数比形参槽数多, 并且在未使用"*identifier"句法的情况下,会引发TypeError异常. 使用该种句法时, 形参接受一个包括有额外位置参数的元组(如果没有额外和位置参数, 它就为空).

If any keyword argument does not correspond to a formal parameter name, a TypeError exception is raised, unless a formal parameter using the syntax "**identifier" is present; in this case, that formal parameter receives a dictionary containing the excess keyword arguments (using the keywords as keys and the argument values as corresponding values), or a (new) empty dictionary if there were no excess keyword arguments.

如果任何一个关键字参数与形参名不匹配, 并且在未使用"**identifier"句法的情况下,会引发TypeError异常. 使用该种句法时, 形参接受一个包括有额外关键字参数的字典(关键字作为键, 参数值作为该键对应的值), 字典如果没有额外和关键字参数, 它就为空.

If the syntax "*expression" appears in the function call, "expression" must evaluate to a sequence. Elements from this sequence are treated as if they were additional positional arguments; if there are postional arguments x1,...,xN , and "expression" evaluates to a sequence y1,...,yM, this is equivalent to a call with M+N positional arguments x1,...,xN,y1,...,yM.

如果在函数调用中使用了"*exprsiones"句法, 那么 "exprsiones"的结果必须是有序类型的. 这个有序类型对象的元素被当作附加的位置参数处理; 如果存在有位置参数x1,...,xN , 并且 "*exprsiones"的计算结果为y1,...,yM, 那么它与具有M+N个参数 x1,...,xN,y1,...,yM 的调用等效.

A consequence of this is that although the "*expression" syntax appears after any keyword arguments, it is processed before the keyword arguments (and the "**expression" argument, if any - see below). So:

由此可以得到一个推论: 尽管"*expression" 句法出现在任何关键字参数之后,但它在处理关键字参数之前计算.(如果有的话,"**expression" 也是如此,参见下述),所以:

>>> def f(a, b):
...  print a, b
...
>>> f(b=1, *(2,))
2 1
>>> f(a=1, *(2,))
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: f() got multiple values for keyword argument 'a'
>>> f(1, *(2,))
1 2

It is unusual for both keyword arguments and the "*expression" syntax to be used in the same call, so in practice this confusion does not arise.

一同使用关键字语法和"*expression" 的情况十分罕见,所以实际上这种混乱是不会发生的.

If the syntax "**expression" appears in the function call, "expression" must evaluate to a (subclass of) dictionary, the contents of which are treated as additional keyword arguments. In the case of a keyword appearing in both "expression" and as an explicit keyword argument, a TypeError exception is raised.

如果在函数调用中使用"**expression"句法,"expression" 计算结果必须是一个字典(的子类).其内容作为附加的关键字参数.如果一个关键字出现在"expression" 中并且是一个显式关键字参数,就会引发TypeError 异常.

Formal parameters using the syntax "*identifier" or "**identifier" cannot be used as positional argument slots or as keyword argument names. Formal parameters using the syntax "(sublist)" cannot be used as keyword argument names; the outermost sublist corresponds to a single unnamed argument slot, and the argument value is assigned to the sublist using the usual tuple assignment rules after all other parameter processing is done.

使用"*identifier"或"**identifier"句法的形参不能作为位置参数或关键字参数名使用.使用"(sublist)"句法的形参也不能作为关键字参数名.最外层的sublist对应一个匿名参数槽,并在处理完其它参数之后使用通常的元组赋值法则将sublist赋为参数值.

A call always returns some value, possibly None, unless it raises an exception. How this value is computed depends on the type of the callable object.

一个元组如果没有引发异常,通常会返回一些值,可能为None. 怎样计算这个值依赖于可调用对象的类型.

If it is--

如果它是 --

用户自定义函数 a user-defined function:
The code block for the function is executed, passing it the argument list. The first thing the code block will do is bind the formal parameters to the arguments; this is described in section 7.5. When the code block executes a return statement, this specifies the return value of the function call.

执行此函数的代码块,并把参数传给它.它要做的第一件事就是将形参与实参对应起来.关于这点参见7.5当代码块执行到 return语句时,会指定这次函数调用的返回值.

内建函数或内建方法 a built-in function or method:
The result is up to the interpreter; see the Python Library Reference for the descriptions of built-in functions and methods.

结果依赖于解释器,详见《Python库参考》

类对象 a class object:
A new instance of that class is returned.

返回该类的一个新实例。

类实例的方法 a class instance method:
The corresponding user-defined function is called, with an argument list that is one longer than the argument list of the call: the instance becomes the first argument.

调用对应的用户自定义函数,其参数个数比普通的函数调用多一:该实例成为方法的第一个参数。

类实例 a class instance:
The class must define a __call__() method; the effect is then the same as if that method was called.

类实例必须定义方法__call__();效果与调用该方法相同。