A special problem is the construction of tuples containing 0 or 1 items: the syntax has some extra quirks to accommodate these. Empty tuples are constructed by an empty pair of parentheses; a tuple with one item is constructed by following a value with a comma (it is not sufficient to enclose a single value in parentheses). Ugly, but effective. For Example
因为:
所以
0 * 5
是0
,而:保持元组的类型。你知道吗
Tuples and Sequences在文档中,一个带有一个项的元组是通过在值后面加逗号来构造的
这是个语法问题。定义元组的不是括号,而是逗号的存在:
(1 + 2) * 3
1, 2, 3
。(1, 2, 3) * 5
是“元组(1,2,3)重复五次”。你知道吗0,
是一个只包含元素0的元组;通常,如果要将此表达式嵌入一个更大的表达式中,您会立即希望将其插入括号,例如(0,) * 5
(“由零组成的元组,乘以5倍”)。而0
和(0)
都表示“整数零”(后一种形式的括号)。你知道吗所以
(0) * 5
是“整数0乘以5”,等于0。任何时候都不涉及元组,因为您使用的语法没有定义元组。你知道吗相关问题 更多 >
编程相关推荐