有人能告诉我为什么,在我运行下面的代码之后,这些是变量现在引用的值:
x = [1, 2, 3, 5]
x1 = [1, 2, 3, 5]
x2 = [1, 2, 3]
y = [1, 2, 3]
y1 = [1, 2, 3]
y2 = [1, 2, 3]
为什么不是y = [1, 2, 3, 5]
?
为什么不是x1 = [1, 2, 3]
?
因为,遵循这个逻辑:
a = 1
b = a
a = 3
b is still equal to 1, not 3.
代码如下。谢谢你的帮助。
def my_function(x, y):
x.append(5)
y = y + [5]
x = [1, 2, 3]
x1 = x
x2 = x[:]
y = [1, 2, 3]
y1 = y
y2 = y[:]
my_function(x, y)