刚刚在Python中遇到了这样一些奇怪的事情,并且认为我会将其记录为一个问题,以防其他任何人试图用相同的无效搜索条件找到答案
看起来像元组解包使得它不能返回长度为1的元组,如果您希望迭代返回值。虽然看起来似乎是欺骗。看到答案。
>>> def returns_list_of_one(a):
... return [a]
...
>>> def returns_tuple_of_one(a):
... return (a)
...
>>> def returns_tuple_of_two(a):
... return (a, a)
...
>>> for n in returns_list_of_one(10):
... print n
...
10
>>> for n in returns_tuple_of_two(10):
... print n
...
10
10
>>> for n in returns_tuple_of_one(10):
... print n
...
Traceback (most recent call last):
File "", line 1, in
TypeError: 'int' object is not iterable
>>>