A slicing selects a range of items in a sequence object (e.g., a string, tuple or list). Slicings may be used as expressions or as targets in assignment or del statements. The syntax for a slicing:
一个片断选择某个有序类型对象(如, 字符串, 元组, 列表)一段范围之内的项. 片断可以作为表达式使用,或者是赋值和del语句的目标. 下面是片断的句法:
slicing |
::= |
simple_slicing |
extended_slicing |
simple_slicing |
::= |
primary "["
short_slice "]" |
extended_slicing |
::= |
primary "["
slice_list "]" |
slice_list |
::= |
slice_item (","
slice_item)* [","] |
slice_item |
::= |
expression |
proper_slice |
ellipsis |
proper_slice |
::= |
short_slice |
long_slice |
short_slice |
::= | [lower_bound]
":" [upper_bound] |
long_slice |
::= |
short_slice ":" [stride] |
lower_bound |
::= |
expression |
upper_bound |
::= |
expression |
stride |
::= |
expression |
ellipsis |
::= | "..." |
There is ambiguity in the formal syntax here: anything that looks like an expression list also looks like a slice list, so any subscription can be interpreted as a slicing. Rather than further complicating the syntax, this is disambiguated by defining that in this case the interpretation as a subscription takes priority over the interpretation as a slicing (this is the case if the slice list contains no proper slice nor ellipses). Similarly, when the slice list has exactly one short slice and no trailing comma, the interpretation as a simple slicing takes priority over that as an extended slicing.
在这里形式句法的说明中有点含糊: 任何看起来像表达式表的语法构件也能看作是片断表, 所以任何下标都可以解释为片断. 但这样要比更复杂的句法要合适, 该定义是没有歧义的, 在这种情况下(在片断表中没有包括适当的片断或者省略写法)优先将其解释为下标, 而不是片断. 类似地, 如果一个片断表很精确地是一个没有后跟逗号的简短片断, 则将其优先解释为简单片断,而不是扩展片断.
The semantics for a simple slicing are as follows. The primary
must evaluate to a sequence object. The lower and upper bound
expressions, if present, must evaluate to plain integers; defaults
are zero and the sys.maxint, respectively. If either
bound is negative, the sequence's length is added to it. The
slicing now selects all items with index k such that
i <= k < j
where i and j are the specified lower and
upper bounds. This may be an empty sequence. It is not an error if
i or j lie outside the range of valid indexes
(such items don't exist so they aren't selected).
一个简单片断的语义如下.主要是要将其导成一个有序类型对象. 如果给出了下限和上限表达式, 它们必须是普通整数; 其默认值为0或sys.maxint, 分别的, 如果其中一个界限为负数, 那么将其与该有序类型对象的长度相加得到新的界限值, 这样片断就是指出了所有的项k, 其中 i <= k < j , i和j是下限和上限.这里允许有空有序类型对象.如果i或j超出索引的合法值并不算是错误(这样的项不存在,所以不被选择).
The semantics for an extended slicing are as follows. The
primary must evaluate to a mapping object, and it is indexed with a
key that is constructed from the slice list, as follows. If the
slice list contains at least one comma, the key is a tuple
containing the conversion of the slice items; otherwise, the
conversion of the lone slice item is the key. The conversion of a
slice item that is an expression is that expression. The conversion
of an ellipsis slice item is the built-in Ellipsis
object. The conversion of a proper slice is a slice object (see
section 3.2)
whose start, stop
and step attributes are the values of the
expressions given as lower bound, upper bound and stride,
respectively, substituting None for missing
expressions.
一个扩展片断的语义如下.主要是要将其导成一个映射对象, 并且由该片断表构成的键作索引. 如果一个片断表包括至少一个逗号, 那么键就是一个包括由片断中的所有项转换而来的元组; 否则, 就用独立的片断项作转换成为键. 为表达式的片断项转换后仍是该表达式, 包括有省略写法的片断项转换后是一个内建的Ellipsis对象. 正常的片断转换后是一个start,stop和step属性为给定的下限,上限和步长的片断对象(见3.2节), 对于缺少的表达式用None替代.
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 |
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--
如果它是 --
执行此函数的代码块,并把参数传给它.它要做的第一件事就是将形参与实参对应起来.关于这点参见7.5当代码块执行到 return语句时,会指定这次函数调用的返回值.
结果依赖于解释器,详见《Python库参考》
返回该类的一个新实例。
调用对应的用户自定义函数,其参数个数比普通的函数调用多一:该实例成为方法的第一个参数。
类实例必须定义方法__call__();效果与调用该方法相同。