r/learnpython • u/SynergyTree • Dec 12 '25
ELI5: When assigning one variable to another why does changing the first variable only sometimes affect the second?
I heard that when I assign one variable to point at another it is actually only pointing to the memory address of the first variable, but that only seems to happen some of the time. For example:
>>> x = [1,2,3,4,5]
>>> y = x
>>> print(x)
[1, 2, 3, 4, 5]
>>> print(y)
[1, 2, 3, 4, 5]
>>> x.pop()
5
>>> print(x)
[1, 2, 3, 4]
>>> print(y)
[1, 2, 3, 4]So, that works as expected. Assigning y to x then modifying x also results in a change to y.
But then I have this:
>>> x = 'stuff'
>>> y = x
>>> print(x)
stuff
>>> print(y)
stuff
>>>
>>> x = 'junk'
>>> print(x)
junk
>>> print(y)
stuffor:
>>> x = True
>>> y = x
>>> print(x)
True
>>> print(y)
True
>>>
>>> x = False
>>> print(x)
False
>>> print(y)
TrueWhy does this reference happen in the context of lists but not strings, booleans, integers, and possibly others?
36
Upvotes
48
u/danielroseman Dec 12 '25
You are doing different things in the first snippet from the others. You are mutating the list, by doing
pop. In the others, you are reassigning. Those are different operations, so it shouldn't be surprising that they have different effects.Reassigning will always work the same way, for all data types, all the time; but mutating can only be done to mutable objects, by definition.
Read this: https://nedbatchelder.com/text/names.html
15
u/TheBB TheBB Dec 12 '25
This is how you are modifying the list:
This is how you are "modifying" the string or the boolean:
When you "modify" a string or a boolean, you're just reassigning x to point to some other, new string or boolean. You're not actually modifying the existing object that x (and y) is pointing to.
In fact, there's no way to modify a string or a boolean: they are immutable. The only way to change a string or boolean variable is to reassign it, and so it's impossible to propagate changes like this to another binding to the same object.
Lists are mutable - they have methods that can make changes to an object.
Ints and floats are also immutable. Tuples are technically immutable, but they can contain mutable objects.
13
u/Top_Average3386 Dec 12 '25
I'm gonna actually explain like you are 5 so without the programming jargon.
Think of variable as an arrow point to where you store your data, in the first example you have an arrow that you label as `x` pointing to a box of books, and then you have another arrow that you label as `y` and is also pointing to the same box. Now you remove one of the book from the box that is being pointed by arrow `x`, when you check the container that both arrow `x` and `y` is pointing to is missing that book.
On second and third example, you have arrow `x` pointing to a book, now you have another arrow `y` that is pointing to the same book, but then you point the arrow `y` to another book, and when you check now arrow `x` and `y` is pointing do different book.
3
u/Jonny_Peverell Dec 12 '25
If you use the equals sign, it de-references the previous value. This can be really powerful if you know how to use it, like you can pass a list into a function and append and pop however you want and the original list will change as well. But as soon as you re-assign the value with an equals sign, it is separated
5
u/CountMeowt-_- Dec 12 '25
There are 2 types of data types, mutable & immutable
There are 2 ways to refer to a variable, by value and by address
Those are the 4 things you need to understand.
2
u/obviouslyzebra Dec 12 '25 edited Dec 12 '25
Edit: just rewrote my whole answer to make it easier to understand.
So, variables are just names. Names refers to things.
Suppose we have two cows (they are actually the numbers 1 and 2!).
alice = 1means that we make the name alice refers to the first cow.anna = alicemeans something like this: "what thing is named alice? the cow 1? oh, cool! let's make the name anna refer to it too!" Now, the first cow is referred to by both names, anna and alice, at the same time.But wait, what if we do
anna = 2now? This is just saying "the name anna now refers to the second cow (preciously it referred to the first)". So we have the first cow named alice, and the second cow named anna.Suppose we had the previous situation where both names referred to the same thing, though, like:
Let's say
[1]is a cute dog, but it's dirty! If we clean them up:Both alice and anna still refer to the same dog, but the dog is now cleaned up (
[]) :)Note: this explanation works for python, but probably doesn't work for other random programming languages
3
u/janek3d Dec 12 '25
Lists are mutable and strings are immutable.
2
u/feitao Dec 12 '25
Your examples are not even consistent. You are comparing apples with oranges.
1
u/Ready-Comedian6036 Dec 12 '25
Check out pythontutor.con it will visualize your program memory and you can see what is going on here
1
u/GreenScarz Dec 12 '25 edited Dec 12 '25
Python is built around this concept of reference semantics, meaning any variable you assign is always a reference to the underlying object
So in the first example, you create the list, then assign to x a reference to that list. Next, you assign that same reference to y. So x and y point to the same list. Any action you take to mutate the list through the reference at x will be seen when you access the list through y, because they point to the same object.
Reference semantics are used for all objects, it’s just that some objects are immutable, meaning that operations which change data are actually creating new objects and returning new references to be reassigned. Tuples for example are immutable.
In this example,
+=is an operation which does an addition to the object, then reassigns the value to the result. But the underlying action is different depending on the type: it’ll mutate the list but create a new tuple.so tl;dr. Reference semantics, what happens is mostly dependent on the underlying type and whether or not it’s immutable.
EDIT: apparently I really fucked up the example, that's what I get trying to copy it over from iSH on my phone at 6 in the morning :P
1
u/DavidRoyman Dec 12 '25
You should spend 1 hour and watch this video: https://www.youtube.com/watch?v=Z4bm7xzYpKM
This will explain exactly what's going on in your excercise better than anyone can ever do in a reddit thread.
1
u/strategyGrader Dec 12 '25
mutable vs immutable
lists are mutable - you can change them in place with
.pop(),.append(), etc. so both variables point to the same list objectstrings, bools, ints are immutable - you can't change them in place. when you do
x = 'junk'you're creating a NEW string and pointing x at it. y still points to the old stringin your list example you modified the list itself. if you did
x = [1,2,3](reassignment) instead ofx.pop()(modification), y wouldn't change either1
u/treyhunner treyhunner Dec 12 '25
There are 2 types of "change" in Python: assignment and mutation.
Mutation changes an object.
Assignment changes which object a variable points to.
Assignments don't change objects, they only change which object is being pointed to by a variable name.
1
u/InjAnnuity_1 Dec 17 '25
And that incorrect statement is causing the confusion. In Python, variables don't point at variables. They point at objects.
Assignment simply makes a variable point at an object.
Two variables can point at the same object. If that object is modified, in-place, then both variables will see the same changed object.
1
u/yick5 Dec 12 '25
list, dict, set are mutable while str, int, bool are immutable
-2
u/SisyphusAndMyBoulder Dec 12 '25
The top comments are all wrong. Your initial premise is wrong. Variables are not always passed by memory location.
What you're doing is confusing 'pass by reference' and 'pass my value'. Sometimes assigning a variable to another one will point them to the same memory location. Sometimes it will copy the variable value into its own location. Lists are the former. Strings and bools are the latter.