不,结果应为1。
将赋值运算符(
=)视为参考的赋值。
a = 1 #a references the integer object 1b = a #b and a reference the same objecta = 2 #a now references a new object (2)print b # prints 1 because you changed what a references, not b
同时,这整个的区别真的是最重要的 可变 对象,如
lists,而不是 一成不变的 状物体
int,
float和
tuple。
现在考虑以下代码:
a=[] #a references a mutable objectb=a #b references the same mutable objectb.append(1) #change b a little bitprint a # [1] -- because a and b still reference the same object # which was changed via b.
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)