r/PythonLearning • u/Sea-Ad7805 • Jul 25 '25
Showcase Name Rebinding
See Solution made using memory_graph.
2
u/ProfessionalFoot736 Jul 28 '25
The weird part here is that x += [y] is not the same as x = x + [y], but is the same as x.append(y). That is pretty counterintuitive and actually surprised me as a long time python dev
1
u/Sea-Ad7805 Jul 28 '25
Mostly the same, here there is a difference: x = ([], []); x[0].append(0); x[1] += [1]
9
u/Constant-Past-6149 Jul 25 '25
C
7
u/Constant-Past-6149 Jul 25 '25
Just for everyone's understanding lets do mental debugging:
a = [1]
b = a = [1] (check both id(b) and id(a), the memory location will be same)
b = a = [1,2]
b = a = [1,2,3]
b = [4] (new memory allocation of b, instead of using a's location, check again both id(b) and id(a) and you'll understand).
b = [4,5]
5
u/professor_coldheart Jul 25 '25
I was sniped by print(a) instead of print(b).
0
Jul 27 '25
[deleted]
1
u/TedditBlatherflag Jul 28 '25
No it’s a terrible exam question. Make the variables legible and clear. If “a” was “foo” and “b” was “bar” then the question is about binding. If it’s “did you notice it said a” it’s just a trick question.
5
u/1000Times2p25 Jul 25 '25 edited Jul 25 '25
C is the answer, because:
b is initially a shallow copy of a (every change of b, impacts a), until the point b = [4].
From that point, b is no longer a shallow copy of a, but instead, a list of its own, with any changes done being directly visible in itself.
EDIT: python interpeter confirms solution C because of the reason above, but I had some doubts at first for answer E for a reason I removed.
EDIT 2: b = a is not a shallow copy, instead, it has to be considered that b is a reference of a
Regards to you all
0
Jul 25 '25
[deleted]
1
u/1000Times2p25 Jul 25 '25
Actually you're right, I though that b = a is also a shallow copy.
Thank you, I will edit the first reply too
1
u/YOM2_UB Jul 25 '25
The answer is A, because b += [2] creates a new list instead of altering the list already stored in b.
2
u/Sea-Ad7805 Jul 25 '25
Incorrect sorry, check the solution or run the code: https://raw.githubusercontent.com/bterwijn/memory_graph_videos/refs/heads/main/exercises/exercise2.py
1
u/NoahZhyte Jul 25 '25
Could you explain? I accept that C is the solution, but I don't understand.
b += [2]should be a reassignement from my knowledge of python0
u/Sea-Ad7805 Jul 26 '25
'b += [2]' changes the value that 'b' is referencing, and that is the same value that 'a' is referencing, and because that value is of mutable type 'list', both 'b' and 'a' are changed, see: https://github.com/bterwijn/memory_graph?tab=readme-ov-file#python-data-model
1
u/niket23697 Jul 26 '25
i thought so too, upon running it i learnt that it's different from doing b = b + [2] TIL
1
u/TheCarter01 Jul 25 '25
Answer is E
1
u/Sea-Ad7805 Jul 25 '25
Incorrect sorry, see the solution, 'b = [4]' results in Name Rebinding, from there 'b' is no longer sharing its value with 'a'.
1
u/SCD_minecraft Jul 25 '25
Iiiii did not know that += works diffrend that just +
What method defines it?
2
1
u/Infamous_Ticket9084 Jul 27 '25
That's really crazy that += is not equivalent to = and +
1
u/Sea-Ad7805 Jul 27 '25
Yes, maybe surprisingly, it changes the value of 'b' but it's not a reassignment (no Name Rebinding, still sharing it's value with 'a').
1
u/Astrophysicist-2_0 Jul 25 '25
A
0
u/Astrophysicist-2_0 Jul 25 '25
You don’t modify it any more after the first line
1
u/Sea-Ad7805 Jul 25 '25 edited Jul 25 '25
'b' is a reference to 'a', and 'a' is of mutable type 'list', so 'a' and 'b' share their value, changing 'b' will change 'a'. Run the code to check: https://raw.githubusercontent.com/bterwijn/memory_graph_videos/refs/heads/main/exercises/exercise2.py
2
1
u/Obvious_Mud_6628 Jul 26 '25
I get why b=a assigns the same memory but I don't get why b=[4] doesn't overwrite that with an entirely new list?
I would think a would recognize changed made to be until b is reassigned to a new list. At which point that link is essentially broken and they're 2 completely separate variables. I.e E
2
0
3
u/Opposite_Ad_6324 Jul 25 '25
That is interesting to learn but also somewhat counterintuitive. So until we give "b" a proper definition it points to the same memory space as "a"?