python函数返回的元组_python – 从函数返回一个单独的元组元组
生活随笔
收集整理的這篇文章主要介紹了
python函数返回的元组_python – 从函数返回一个单独的元组元组
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
剛剛在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
>>>
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結
以上是生活随笔為你收集整理的python函数返回的元组_python – 从函数返回一个单独的元组元组的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 国产手机普及24GB大内存!最高兴的是A
- 下一篇: 李小龙 IP 动画《House of L