python - 为什么单个元素元组被解释为 python 中的那个元素?

标签 python printf tuples convention

谁能解释为什么单元素元组在 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.

Source

唯一没有逗号的元组是 0 元组,即 () .请注意,您可以通过运行 type((1)) 来检查这一点并看到它返回 <type 'int'>不是<type 'tuple'> .

关于python - 为什么单个元素元组被解释为 python 中的那个元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40710455/

相关文章:

python - 使用 keras 训练时出现奇怪的结果

python - 如何将数据附加到 Excel 文件的最后一行(每次)?

python - Faster R-CNN torchvision 实现的澄清

c - 为什么这个 C 代码不工作?

python - 元组列表中元组的第一个元素小写

python - 如何按反向排序元组,但不反向打破关系? (Python)

python - 如何根据变量指定 GAE Python ndb 字段名称

c - 如何通过在 printf ("%ex",test()) 中定义新标识符来启用 printf() 函数以在 C 中打印 boolean 类型?

ocaml sprintf 说明

list - F# 拆分列表