0

I apologize for the naive nature of the question. Consider the python code below and its output on the right:

enter image description here

I was under the impression that the output should be:

[2,3,10,5,8]

[2,3,4,5,8]

I thought that modifying the variable y will not modify x. Apparently, this is not true. How can I write code that allows me to modify y without modifying x.Thank you

Surprisingly, I don't get this behaviour when the data type is numbers. See the python code below:

enter image description here

Amr
  • 101
  • 3
  • You have a *single* list object here. It is being referenced by two variables, so of course if you mutate that list, it will be mutated. – juanpa.arrivillaga Jun 14 '22 at 21:03
  • 1
    It seems you assumed that `y = x` creates a copy, but it doesn't. No copies are made by that statement. It means something like "the object that results from evaluating `x` is now referenced by the name `y`", note, this has nothing to do with "memory locations", which is a level of abstraction that is usually not helpful in Python – juanpa.arrivillaga Jun 14 '22 at 21:03
  • There is a similar question [addressed here](https://stackoverflow.com/questions/20483973/python-mutating-the-copy-of-a-list-changes-the-original) – Oluwafemi Sule Jun 14 '22 at 21:04
  • 2
    In any case, you should just read the following: https://nedbatchelder.com/text/names.html – juanpa.arrivillaga Jun 14 '22 at 21:04
  • y is the same list as x, just by another name, you need to make a copy of it if you want y to be a different list, like for example `y=list(x)` – Copperfield Jun 14 '22 at 21:05
  • But in short, `x` and `y` are **not memory locations** (like variables are in languages like C), `x` and `y` are *names* in a namespace (the global namespace here) which can refer to any arbitrary object – juanpa.arrivillaga Jun 14 '22 at 21:05
  • @juanpa.arrivillaga Thanks. Can you please look at the new edit to my post – Amr Jun 14 '22 at 21:10
  • 2
    "Surprisingly, I don't get this behaviour when the data type is numbers. See the python code below:" Because you are doing a completely different thing. The data type is irrelevant here. Consider: `x = []; y = x; y = [1,2]; print(x, y)` and you'll see the same behavior. Assignment *does not mutate*. Again, you **really** should read https://nedbatchelder.com/text/names.html – juanpa.arrivillaga Jun 14 '22 at 21:15
  • @juanpa.arrivillaga Thanks for the link, I ll check it. – Amr Jun 14 '22 at 21:16
  • @juanpa.arrivillaga Can you elaborate why I am doing a completely different thing ? In both cases, I first define a variable x, then define y to be x, then modify the value of y , then print both x,y – Amr Jun 14 '22 at 21:18
  • the difference in that you do `y[2]` first before the `=` in your first example `y[2]=10`, which mean in the list that the variable y is pointing to, in the positing 2 insert the value 10, and it also happens that both x and y both refer to the same underlying list, and that is why both can see the change. Just plain `y=3` in the second one means the variable y is reassigned to the integer object "3" – Copperfield Jun 14 '22 at 21:29
  • [this](https://www.youtube.com/watch?v=_AEJHKGk9ns) is video version of @juanpa.arrivillaga link, if you prefer it in that way – Copperfield Jun 14 '22 at 21:31
  • @copperfield Thank you for your help. I will check the sources you referenced, they seem to answer my questions. Have a great day – Amr Jun 14 '22 at 21:38
  • @Copperfield so to elaborate, `y[2] = 2`, is essentially sugar for `y.__setitem__(2, 2)` which is a method call, that in this case, is a mutator method – juanpa.arrivillaga Jun 14 '22 at 21:50
  • [Please do not upload images of code/errors when asking a question.](//meta.stackoverflow.com/q/285551) – mustaccio Jun 15 '22 at 18:58

0 Answers0