gpt4 book ai didi

变量的Python含义后跟逗号和换行符

转载 作者:太空宇宙 更新时间:2023-11-04 09:27:00 27 4
gpt4 key购买 nike

当我有一个变量后跟一个逗号时,我看到了一些奇怪的打印输出,我想知道 Python 解释器在遇到它时到底做了什么。

def new_func():
x = 1
y = 2
return x,y

x = 0
x,
y = new_func()

print(x,y)

输出:0 (1,2)

那么到底打印了什么? Python 是如何处理 x,↵ 的?我可以用它做什么?

最佳答案

一般来说,在 Python 中,逗号使某些东西成为元组。换行符没有任何作用。

例如i = 1,等价于i = (1,)

一般示例:

>>> 1
1
>>> 1,
(1,)

您的情况:

>>> x = 0

>>> type(x)
int

>>> x,
(0,) # this is just the printed version of x as tuple

>>> x = x, # actual assignment to tuple

>>> type(x)
tuple

关于变量的Python含义后跟逗号和换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57171050/

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