1
a = [1,2,3]
b = [a.copy(),a]
c = b.copy()

print(b[0] is c[0])
>>> True
print(c[1] is a)
>>> True

Hi, I've been trying out shallow copying and I don't really understand why b[0] is c[0]. If c is a shallow copy of b, shouldn't it be independent of it? I've tried visualising with pythontutor but I still don't get it.

Thanks in advance

asd-qwert
  • 41
  • 5
  • 2
    "If c is a shallow copy of b, shouldn't it be independent of it?", No, a shallow copy, by definition, only copies the *container*, not the items in the container – juanpa.arrivillaga Nov 22 '21 at 01:33
  • 1
    And note, *variables* aren't copied. *Objects* are. – juanpa.arrivillaga Nov 22 '21 at 01:34
  • 1
    Does this answer your question? [What is the difference between shallow copy, deepcopy and normal assignment operation?](https://stackoverflow.com/questions/17246693/what-is-the-difference-between-shallow-copy-deepcopy-and-normal-assignment-oper) – rustyhu Nov 22 '21 at 01:43
  • 1
    You already have good answers ^^^above. But if you like to `visualize` the things - `a` and `b` or `c`. You can try this great platform https://pythontutor.com/ – Daniel Hao Nov 22 '21 at 01:56
  • You might benefit from watching Ned Batchelder's [video about python names](https://www.youtube.com/watch?v=_AEJHKGk9ns). – joseville Nov 22 '21 at 02:08

0 Answers0