【发布时间】:2012-10-12 12:51:54
【问题描述】:
我在这里更新 x,如果我在更新 x 后打印 y,y 将显示反射。但是,在第一个示例中, y 没有显示出这种变化。这就是我迷路的地方。谁能给我解释一下?
x = 2
y = x + 3
### printing y will yield 5
x = 8 # ok am I updating the same storage space that x pointed to, or is abandoning it and creating
# another with a value of 8?
### printing y will still yield 5 (why is that? if Y always points to x and adds 3 to it?)
in contrast with this:
x = [1,2,3]
y = [x,4,5,6]
x[0] = 50
y[2] = 80
z = y[:] + [100,101,1020
【问题讨论】:
-
将
x[0]=50视为x.__setitem__(0, 50)的简写。不要让赋值运算符让你失望。
标签: python variables global-variables