项目中使用python tuple时如果是一个元素一定要增加一个逗号,使用python3看下输出。
>>> t(0) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 't' is not defined >>> t=(0) >>> print(type(t)) <class 'int'> >>> print(t) 0 >>> t=(0,) >>> print(type(t)) <class 'tuple'> >>> print(t) (0,) 官方文档中描述:
Note that it is actually the comma which makes a tuple, not the parentheses. The parentheses are optional, except in the empty tuple case, or when they are needed to avoid syntactic ambiguity. For example, f(a, b, c) is a function call with three arguments, while f((a, b, c)) is a function call with a 3-tuple as the sole argument.
...