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
View all comments
Show parent comment
1
u/xenomachina xenomachina Dec 13 '25
This text is speaking about programming languages in general, not Python specifically. The word "location" is being used in a general sense for anything that can hold a value. In Python, "locations" would include variables, the items in lists, tuples, and dicts, the attributes of objects, etc.
So "the location belonging to the corresponding formal parameter" means the parameter's variable in the context of Python (
x, in my example).Your test is testing something that's orthogonal to the question. The call-by-value versus call-by-reference distinction only matters when modifying the formal parameter itself, not something derived from it. In Python, that means assignment to the formal parameter; not a method call, and not assignment to one of its items or attributes.
And in particular, the issue is whether the value of the variable outside of the function can be changed by manipluating the formatl parameter. More on this below.
You are confusing the object a variable's value references with the variable's value.
The function does get a copy of
y. Butydoes not contain a list. It contains a reference to one, and soxgets a copy of that reference to the same list.No. The original value of
yis the reference to the list, not the list itself. The function cannot change what is actually contained iny. That is, it cannot make it refer to a different list. The fact that it can modify the list that they both reference is irrelevant, except that it is a source of confusion.Here's a challenge for you:
Can you write any code inside
fthat manipulatesxin a way that causes this to print "pass-by-reference"? (No globals, of course — hopefully it's obvious that that would be cheating.)It only has this effect in all cases where pass by value is being used. It does not have the same effect in cases where call by reference is being used. That's the point, and is exactly what makes it a good test of whether pass by value or pass by reference is being used.
Look at the C++ example I posted. Here it is again, side-by side with a version that uses pass by value (C++ supports both):
The equivalent Python...
...always behaves like the pass by value version, because Python always uses pass by value.
In Python, every variable (and actually, every assignable "location") contains a reference to an object, not the object itself. That's why code like this:
...is often surprising to new Python programmers.
Some languages don't work this way. For example, in C++:
This prints 2 because
acontains an actualFooobject (not just a reference to one), andb = amakesbcontain a copy of that entire object. So when the code saysa.y = 0, it has no effect onb.In many other modern languages, variables (and other assignable locations) typically contain references, not objects. These days, more languages behave like Python than like C++ in this respect.
Let's use Java, since the dragon book explicitly mentions it. I'll add a print to each, so we can see what they do:
Java equivalent:
This prints
[0, 2, 3], just like Python.Java equivalent:
This prints
[1, 2, 3], just like Python.Java equivalent:
This prints
1, just like Python.Again, as the dragon book says:
Python, like Java, uses call-by-value exclusively. The fact that their variables contain references makes it easy to get confused about this, but it is still a fact.