gpt4 book ai didi

python - python中元组构造函数的目的

转载 作者:行者123 更新时间:2023-12-05 00:46:15 25 4
gpt4 key购买 nike

我最近遇到了 tuple() 作为 python 中的构造函数,如果仅使用 ()tuple() 之间存在差异>,这是什么?

最佳答案

tuple 是一种类型。

>>> tuple
<class 'tuple'>

当你调用类型时,你可以传递任何类型的可迭代对象来取回一个包含可迭代对象元素的非空元组。

>>> tuple([1,2,3])
(1, 2, 3)
>>> tuple((1,2,3))
(1, 2, 3)
>>> tuple(i+1 for i in range(3))
(1, 2, 3)

如果不传递参数,则创建一个空元组。

>>> tuple()
()

() 是一个计算结果为空元组的文字,就像您调用没有参数的类型一样。

>>> ()
()
>>> tuple() == ()
True

关于python - python中元组构造函数的目的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61704534/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com