2
foo = [1, 2, 3]  
foo[:][0] = 5

foo doesn't change, also:

import copy  
foo = [1, 2, 3]   
boo = copy.copy(foo)  
boo[0] = 5

Again, foo[0] stays the same.

Why? The shallow copy creates new list, but shouldn't boo[0]/boo[1]/boo[2] point to the same objects as foo[0]/foo[1]/foo[2]?

das-g
  • 9,718
  • 4
  • 38
  • 80
  • You are just reassigning what's stored at position 0 in your copy, why would that change the original? – timgeb Dec 11 '15 at 20:01
  • 3
    I don't think the question that was marked duplicate is really a duplicate. This question is not asking how to make a copy of a list, it's asking why a copy of a list doesn't work in the way the questioner expects. – BrenBarn Dec 11 '15 at 20:03
  • related http://stackoverflow.com/questions/3485475/can-i-create-a-view-on-a-python-list – wim Dec 11 '15 at 20:05
  • @BrenBarn Sure. I felt that the OPs question was answered in the dupe So I used the hammer. I apologize for that. Regards. – Bhargav Rao Dec 11 '15 at 20:05
  • 1
    Why would you expect foo to change when you are copying it? – Padraic Cunningham Dec 11 '15 at 20:10
  • @PadraicCunningham I'm just trying to understand how it works - I'm used to c++ mechanisms, which are very intuitive. Here the diffrence is subtle yet important and I'm trying to get it. –  Dec 11 '15 at 20:18
  • @JohnnyJohansson, if you look at it logically, if `copy.copy` on a shallow list with no other mutable objects inside still meant changes were reflected in the original when the copy was mutated then copy.copy would basically be doing nothing. What differs in c++? – Padraic Cunningham Dec 11 '15 at 20:22
  • @PadraicCunningham doing nothing? it would create NEW object. My understanding was that if you would edit any of the items that were copied (boo[0/1/2]), the foo would change as well. But if you would for example append something to boo, foo wouldn't change. –  Dec 11 '15 at 20:39
  • yes but `foo` would be `boo` so you would just have the same object not a new one. – Padraic Cunningham Dec 11 '15 at 20:41
  • no. they would only store 3 mutual integer objects - 1,2,3. –  Dec 11 '15 at 20:43
  • @JohnnyJohansson, lists store references to object, you are creating new objects/ints as ints are immutable, the only way you would be sharing references/objects would be if the objects inside the list were mutable, i.e other lists in which case you would `copy.deepcopy`. Apply the same logic to `[[1,2],[3,4]]` – Padraic Cunningham Dec 11 '15 at 20:48
  • Yes, and that's what I've been missing :d The diffrence between c++ and Python I struggle with is that in Python variable isn't really variable, it's a name assigned to an object, and this is very unusual for me so far. –  Dec 11 '15 at 20:54
  • @JohnnyJohansson, there are a few gotchas in python especially coming from other languages but overall it is a pretty easy language to use, everything is an object! – Padraic Cunningham Dec 11 '15 at 21:02

1 Answers1

3

boo[0] does point to the same object as foo[0]. But doing boo[0] = 5 does not modify the object referred to by boo[0]; it modifies the object referred to by boo.

Assigning to an element of a list modifies the list by changing what that element "points to". It has no effect on the object that is pointed to.

BrenBarn
  • 242,874
  • 37
  • 412
  • 384