从函数返回包含单个项目的元组

发布于 2024-11-24 02:59:49 字数 752 浏览 3 评论 0原文

刚刚在 Python 中遇到了这一点奇怪的地方,我想我应该记录它 在这里将其写为一个问题,以防其他人试图使用与我相同的毫无结果的搜索词找到答案。 was

看起来元组解包使得如果您希望迭代返回值,则无法返回长度为 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 "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
>>>

Just came across this little bit of weirdness in Python and thought I'd document it write it as a question here in case anyone else is trying to find an answer with the same fruitless search terms I was

Looks like tuple unpacking makes it so you can't return a tuple of length 1 if you're expecting to iterate over the return value. Although it seems that looks are deceiving. See the answers.

>>> 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 "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
>>>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦境 2024-12-01 02:59:50

您需要显式地将其设为元组(请参阅官方 教程):

def returns_tuple_of_one(a):
    return (a, )

You need to explicitly make it a tuple (see the official tutorial):

def returns_tuple_of_one(a):
    return (a, )
韵柒 2024-12-01 02:59:50

这不是一个错误,一元组是由 val,(val,) 构造的。在 python 语法中定义元组的是逗号而不是括号。

您的函数实际上返回 a 本身,这当然是不可迭代的。

引用序列和元组文档

一个特殊的问题是包含 0 或 1 的元组的构造
items:语法有一些额外的怪癖来适应这些。空的
元组由一对空括号构造;一个元组与
一项是通过在值后面加上逗号来构造的(它不是
足以将单个值括在括号中)。丑陋,但是
有效。

This is not a bug, a one-tuple is constructed by val, or (val,). It is the comma and not the parentheses that define the tuple in python syntax.

Your function is actually returning a itself, which is of course not iterable.

To quote sequence and tuple docs:

A special problem is the construction of tuples containing 0 or 1
items: the syntax has some extra quirks to accommodate these. Empty
tuples are constructed by an empty pair of parentheses; a tuple with
one item is constructed by following a value with a comma (it is not
sufficient to enclose a single value in parentheses). Ugly, but
effective.

三生一梦 2024-12-01 02:59:50

(a) 不是单个元素元组,它只是一个带括号的表达式。使用(a,)

(a) is not a single element tuple, it's just a parenthesized expression. Use (a,).

本宫微胖 2024-12-01 02:59:50

您可以使用 tuple() 内置方法来代替那个丑陋的逗号。

def returns_tuple_of_one(a):
    return tuple(a)

Instead of that ugly comma, you can use the tuple() built-in method.

def returns_tuple_of_one(a):
    return tuple(a)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文