In the int example, you first assign the same object to both a
and b
, but then reassign a
with another object (the result of a+1
). a
now refers to a different object.
In the list example, you assign the same object to both a
and b
, but then you don't do anything to change that. append
only changes the interal state of the list object, not its identity. Thus they remain the same.
If you replace a.append(1)
with a = a + [1]
, you end up with different object, because, again, you assign a new object (the result of a+[1]
) to a
.
Note that a+=[1]
will behave differently, but that's a whole other question.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…