A list display is a possibly empty series of expressions enclosed in square brackets:
一个列表用一对方括号括住的表达式序列(可能为空)表示:
test |
::= |
and_test ( "or"
and_test )* |
lambda_form |
testlist |
::= |
test ( ","
test )* [ "," ] |
list_display |
::= | "[" [listmaker]
"]" |
listmaker |
::= |
expression (
list_for | ( ","
expression )* [","] ) |
list_iter |
::= |
list_for |
list_if |
list_for |
::= | "for"
expression_list "in"
testlist [list_iter] |
list_if |
::= | "if"
test [list_iter] |
A list display yields a new list object. Its contents are specified by providing either a list of expressions or a list comprehension. When a comma-separated list of expressions is supplied, its elements are evaluated from left to right and placed into the list object in that order. When a list comprehension is supplied, it consists of a single expression followed by at least one for clause and zero or more for or if clauses. In this case, the elements of the new list are those that would be produced by considering each of the for or if clauses a block, nesting from left to right, and evaluating the expression to produce a list element each time the innermost block is reached.
使用一个列表会生成一个新的列表对象.它的值由表达式表或由深列表给出.当给出一个逗号分隔的表达式表时, 从左到右地对每个元素求值然后按顺序放进列表对象中. 如果给出的是深列表, 它由至少是一个for子句以及后跟零个或多个for/if子句构成的一个表达式组成. 此时, 新列表的元素由每个for或if子句块决定, 嵌套是从左至右方向的, 而且每执行到最内部的语句块就产生一个列表元素.
A dictionary display is a possibly empty series of key/datum pairs enclosed in curly braces:
一个字典用一对大括号括住的 键/数据 对的序列(可能为空)表示:
dict_display |
::= | "{" [key_datum_list]
"}" |
key_datum_list |
::= |
key_datum (","
key_datum)* [","] |
key_datum |
::= |
expression ":"
expression |
A dictionary display yields a new dictionary object.
使用一个字典会生成一个新的字典对象.
The key/datum pairs are evaluated from left to right to define the entries of the dictionary: each key object is used as a key into the dictionary to store the corresponding datum.
键/数据对按在字典中定义的从左到右的顺序求值:每个键对象作为键嵌入到字典中存储相应的数据.
Restrictions on the types of the key values are listed earlier in section 3.2. (To summarize,the key type should be hashable, which excludes all mutable objects.) Clashes between duplicate keys are not detected; the last datum (textually rightmost in the display) stored for a given key value prevails.
关于键值类型的限制已在前述3.2提及(总而言之,键的类型应该是可散列的,这就排除了所有的可变对象)。重复键之间的冲突不会被检测到;对给定的(有重复的)键来说,最后出现的数据(就是文字显示中出现在最右边的)成为(最终的)胜利者。
A string conversion is an expression list enclosed in reverse (a.k.a. backward) quotes:
一个串转换是由一对反引号(`)引用的表达式表.
A string conversion evaluates the contained expression list and converts the resulting object into a string according to rules specific to its type.
串的转换先计算所包括的表达式表的值,然后按照其结果类型所特定的规则转换其结果对象为字符串.
If the object is a string, a number, None, or a
tuple, list or dictionary containing only objects whose type is one
of these, the resulting string is a valid Python expression which
can be passed to the built-in function eval() to yield an expression with the same value
(or an approximation, if floating point numbers are involved).
如果结果对象属于字符串, 数值, None, 或元组,字典其中的一个类型, 那么结果串就是可以通过传递给内建函数eval()来生成一个具有与原值相同的正确的Python表达式(或, 对于浮点数来说生成的是近似数).
(In particular, converting a string adds quotes around it and converts ``funny'' characters to escape sequences that are safe to print.)
(特别地, 经增加反引号转换的字符串和由特殊字符串所转换得到的转义字符串是能够显示输出的.)
Recursive objects (for example, lists or dictionaries that contain a reference to themselves, directly or indirectly) use "..." to indicate a recursive reference, and the result cannot be passed to eval() to get an equal value (SyntaxError will be raised instead).
试图转换一个递归对象是无效的(例如, 一个直接或间接地包含有对自身引用的列表或字典.)
The built-in function repr() performs exactly the same conversion in its argument as enclosing it in parentheses and reverse quotes does. The built-in function str() performs a similar but more user-friendly conversion.
内建函数repr()作相同的转换: 将其括号中的参数转换成用反引号引用它后的结果. 内建函数str()与之类似,但结果更具可读性.