python - changing the value of var1 -


this question has answer here:

my question more of understanding question strict programming question. know python variables pointers, means don't store value, rather point place in memory value stored. can't figure out how following 2 cases differ:

>>> = 3 >>> b = >>> 3 >>> b 3 >>>b = 4 >>> b 4 >>> 3 

the new value assigned 'b' not change value pointed 'a'. oppose to:

>>> = [1,2,3] >>> b = >>> [1,2,3] >>> b [1,2,3] >>> b.append(4) >>> b [1,2,3,4] >>> [1,2,3,4] 

the new value assigned b changed value pointed a

calling b.append doesn't assign b new list. still points same position in memory.

>>> b = [1,2,3] >>> id(b) 36586568l >>> b.append(4) >>> id(b) 36586568l 

since underlying data changes, other identifiers pointing data affected.


Comments