参见英文答案 >
When are parentheses required around a tuple? 3个
前段时间我认为元组的构造函数是一对圆括号().
例:
>>> (1, )
(1, )
>>> type((1, ))
>>> t = (1, )
>>> type(t)
但现在我知道它是逗号,.
所以,做同上面的事情:
>>> 1,
(1,)
>>> type(1,)
# Why?
>>> 1,2,3
(1,2,3)
但如果我这样做:
>>> type(1,2,3)
Traceback (most recent call last):
File "", line 1, in
TypeError: type() argument 1 must be string, not int
这对我来说很有意义,但是:
>>> t = 1,2,3
>>> type(t)
最后:
>>> type((1,2,3))
这里有一个问题:如果元组只有1,2,3,为什么在最后的情况下需要括号?