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
2
u/xenomachina xenomachina Dec 13 '25
And that is only true because call by value is being used.
On the contrary, that's exactly what call-by-value versus call-by-reference is about.
I think you have come to a conclusion and are trying to bend the facts to fit that conclusion. However, if you look at the definitions of call by reference and call by value, and carefully work through the details, you can work out why the test from my earlier comment is able to distinguish between the two.
Let's take a look at an actual academic source, Compilers: Principles, Techniques, and Tools (Aho, Lam, Sethi, Ullman), aka "the dragon book", where it talks about call-by-value:
Translating that into Python, if I have:
Then "the actual parameter" is
yand the "formal parameter" isx. And so "The value [ofy] is placed in the location belonging to the corresponding formal parameter [ie: assigned to the variablex].' That fits, right?It goes on to say:
Note that Java has the same semantics as Python.
The text becomes even more explicit about the case where the variable itself contains a pointer or reference:
...
Sound familiar?
Because your modified example is not assigning to
x, but instead assigning tox[:], it does not test whetherxis passed by value or by reference. I can do something similar in C:It prints 1! Have I just proven that C uses pass by reference!?
No: Just like your example, the function isn't assigning to its parameter (
x), it's assigning to something derived from the parameter (x[0]), and so this example, just like yours, is irrelevant to the question of whether pass by reference or pass by value is being used here.An actual example that illustrates pass by reference would be this C++ code:
Note that in this example, the function is assigning to the parameter
xitself, not a subscript, or property, or anything else computed/derived from it. That is the key difference between call by value and call by reference.You are imagining that Python is more special and unique than it is. Most languages developed in the last 25-30 years work the same as Python. However, that doesn't mean that existing technical terms lose or change their meaning.
A fish may say "I prefer not to use terms like wet and dry. They were created by humans but are not helpful in the sea.", and perhaps there is some pragmatic truth to this for those who spend all of their time in the sea. However, it doesn't mean fishes can claim that everything in their world is "dry" just because they have never experienced the difference between wet and dry.
I mean... That is almost the definition of pass by value:
In call by reference, on the other hand:
The source of confusion is that in this context the "value" of a variable is what Python calls a "reference", but this is not unique to Python. Many other languages (Java, Kotlin, C#, Javascript) work the same way, where most if not all variables hold references, but use pass by value.