5 ms·
You definitely notice the difference when dealing with mutable types in python, like lists. >> a = [1,2,3] >> b = a >> a.append(4) >> b #[1,2,3,4] Thi
by jpace121 13y ago
You definitely notice the difference when dealing with mutable types in python, like lists.
>> a = [1,2,3]
>> b = a
>> a.append(4)
>> b #[1,2,3,4]
This is one of the things that throws off many beginners, and is an easy trap to fall into, not exotic at all.
- beambot 13y agoOP literally says this in the next sentence: > But if a and b are both variables of the same Java class type, or Python lists, then after the assignment they refer to the same underlying object, so that if you make a change to a (e.g. by calling a class method on it, or appending an item to the list) then you see the same difference when you look at b.
- rcxdude 13y agoHis point is that there isn't a difference in python: in both cases you're just changing labels, but in the case of integers they're pointing to immutable objects.
- jffry 13y agoRight, I was talking specifically about the integer case in Python
- kyllo 13y agoThat's why understanding when variable assignment gives you a reference vs. a copy is absolutely crucial to working with object-oriented languages.