gpt4 book ai didi

python - x,y = getPos() 与 (x, y) = getPos()

转载 作者:太空狗 更新时间:2023-10-30 00:22:05 25 4
gpt4 key购买 nike

考虑这个返回元组的函数 getPos()。以下两个作业有什么区别?我在某个地方看到了一个例子,其中使用了第一个分配,但是当我尝试第二个分配时,我很惊讶它也有效。那么,真的有区别吗,还是 Python 只是弄清楚左边的部分应该是一个元组?

def getPos():
return (1, 1)

(x, y) = getPos() # First assignment
x, y = getPos() # Second assignment

最佳答案

了解 tuples :

A tuple consists of a number of values separated by commas (...)

因此括号不会使元组成为元组。逗号做到了。

只有当你有奇怪的嵌套结构时才需要括号:

x, (y, (w, z)), r

关于python - x,y = getPos() 与 (x, y) = getPos(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3941407/

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