谁能解释为什么单元素元组在 Python 中被解释为该元素?
和
他们为什么不将元组 (1,)
打印为 (1)
?
请看下面的例子:
>>> (1)
1
>>> ((((1))))
1
>>> print(1,)
1
>>> print((1,))
(1,)
最佳答案
是因为(1)
不是元组。 (1)
是1
用括号括起来。正如 python 文档所述
it is the comma, not the parentheses, that define the tuple.
唯一没有逗号的元组是 0 元组,即 ()
.请注意,您可以通过运行 type((1))
来检查这一点并看到它返回 <type 'int'>
不是<type 'tuple'>
.
关于python - 为什么单个元素元组被解释为 python 中的那个元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40710455/