为什么单个元素元组被解释为 python 中的那个元素?
Why single element tuple is interpreted as that element in python?
谁能解释为什么单元素元组被解释为 Python 中的那个元素?
和
他们为什么不将元组 (1,)
打印为 (1)
?
请参阅以下示例:
>>> (1)
1
>>> ((((1))))
1
>>> print(1,)
1
>>> print((1,))
(1,)
因为在你的例子中只有 (1, )
是元组。其余为表达式。
In [4]: type(1,)
Out[4]: int
In [5]: type((1,))
Out[5]: tuple
In [6]: type((1))
Out[6]: int
因为(1)
不是元组。 (1)
是用括号括起来的 1
。正如 python 文档所述
it is the comma, not the parentheses, that define the tuple.
唯一没有逗号的元组是 0 元组,即 ()
。请注意,您可以通过 运行 type((1))
进行检查,发现它 returns <type 'int'>
而不是 <type 'tuple'>
.
(1)
不是元组,它只是数字周围的括号。这是因为有时您想使用括号来表示运算顺序,例如:(x+y)*z。这显然不是指包含 x+y
的元组,它只是为了表明加法应该在乘法之前发生。
出于同样的原因,(((1)))
不是元组,括号只是在说 "evaluate what's inside before moving on".
print(1,)
只是在数字 1 上调用打印函数。调用函数时,允许使用尾随逗号。但是,在 python2 中,这可能会触发 (1,)
,因为 print
不是函数。
print((1,))
是唯一打印元组的东西,因为我们现在实际上是将元组传递给函数。
这非常详细 来自@Ray Total
Sure, in general parentheses don't change the meaning of an expression. For example you can say 4+(1) and it will be 5, the same way 4*(2-1) would be 4. Because the convention is to use parentheses for grouping of subexpressions, the designer of Python thought it would be too confusing to overload the meaning to mean both grouping and single-element tuples. Also Python has a type function. In fact type((2)) is int and type((2,)) is tuple. We don't want there to be any ambiguity, which there would be if (2) were treated as a tuple
单个元素元组永远不会被视为包含的元素。括号主要用于分组,而不是用于创建元组;逗号可以做到这一点。
Why don't they just print (1,) as (1)?
可能是因为打印内置容器类型给出了可用于通过 重新创建容器对象的表示,比如 eval
:
__repr__
的文档对此提供了一些说明:
If at all possible, this should look like a valid Python expression
that could be used to recreate an object with the same value
回答您的问题,(1)
只是带有分组括号的整数 1
。为了通过其表示重新创建单例元组,它必须打印为 (1,)
,这是创建元组的有效语法。
>>> t = '(1,)'
>>> i = '(1)'
>>> eval(t)
(1,) # tuple
>>> eval(i)
1 # int
一个元组由许多用逗号分隔的值组成(不需要括号)。
你甚至可以这样定义元组
t = 1, # (with or without surrounding parentheses)
>>> type(t)
<class 'tuple'>
此处,
用于告诉解释器创建元组,如果不存在,它将被视为int
。
同样的规则适用于我们这样定义
>>> t = (1)
>>> type(t)
<class 'int'>
谁能解释为什么单元素元组被解释为 Python 中的那个元素?
和
他们为什么不将元组 (1,)
打印为 (1)
?
请参阅以下示例:
>>> (1)
1
>>> ((((1))))
1
>>> print(1,)
1
>>> print((1,))
(1,)
因为在你的例子中只有 (1, )
是元组。其余为表达式。
In [4]: type(1,)
Out[4]: int
In [5]: type((1,))
Out[5]: tuple
In [6]: type((1))
Out[6]: int
因为(1)
不是元组。 (1)
是用括号括起来的 1
。正如 python 文档所述
it is the comma, not the parentheses, that define the tuple.
唯一没有逗号的元组是 0 元组,即 ()
。请注意,您可以通过 运行 type((1))
进行检查,发现它 returns <type 'int'>
而不是 <type 'tuple'>
.
(1)
不是元组,它只是数字周围的括号。这是因为有时您想使用括号来表示运算顺序,例如:(x+y)*z。这显然不是指包含 x+y
的元组,它只是为了表明加法应该在乘法之前发生。
(((1)))
不是元组,括号只是在说 "evaluate what's inside before moving on".
print(1,)
只是在数字 1 上调用打印函数。调用函数时,允许使用尾随逗号。但是,在 python2 中,这可能会触发 (1,)
,因为 print
不是函数。
print((1,))
是唯一打印元组的东西,因为我们现在实际上是将元组传递给函数。
这非常详细
Sure, in general parentheses don't change the meaning of an expression. For example you can say 4+(1) and it will be 5, the same way 4*(2-1) would be 4. Because the convention is to use parentheses for grouping of subexpressions, the designer of Python thought it would be too confusing to overload the meaning to mean both grouping and single-element tuples. Also Python has a type function. In fact type((2)) is int and type((2,)) is tuple. We don't want there to be any ambiguity, which there would be if (2) were treated as a tuple
单个元素元组永远不会被视为包含的元素。括号主要用于分组,而不是用于创建元组;逗号可以做到这一点。
Why don't they just print (1,) as (1)?
可能是因为打印内置容器类型给出了可用于通过 重新创建容器对象的表示,比如 eval
:
__repr__
的文档对此提供了一些说明:
If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value
回答您的问题,(1)
只是带有分组括号的整数 1
。为了通过其表示重新创建单例元组,它必须打印为 (1,)
,这是创建元组的有效语法。
>>> t = '(1,)'
>>> i = '(1)'
>>> eval(t)
(1,) # tuple
>>> eval(i)
1 # int
一个元组由许多用逗号分隔的值组成(不需要括号)。
你甚至可以这样定义元组
t = 1, # (with or without surrounding parentheses)
>>> type(t)
<class 'tuple'>
此处,
用于告诉解释器创建元组,如果不存在,它将被视为int
。
同样的规则适用于我们这样定义
>>> t = (1)
>>> type(t)
<class 'int'>