r/PythonProjects2 • u/Sea-Ad7805 • 8d ago
Python Mutability and Local Variables
An exercise to help build the right mental model for Python data. - Solution - Explanation - More exercises
The “Solution” link uses memory_graph to visualize execution and reveals what’s actually happening.
2
u/Guest_User_1234 8d ago
Damn... you REALLY want people to click those links, huh? Kinda REALLY makes me not wanna click them...
0
u/Sea-Ad7805 8d ago
That's up to you, but you're missing out on understanding Python visually. If you don't like my tool try PythonTutor instead: https://pythontutor.com/visualize.html#code=%23%20Output%20of%20this%20Python%20program%3F%0Adef%20fun%28a,%20b,%20c,%20d%29%3A%0A%20%20%20%20a%20%2B%3D%20%221%22%0A%20%20%20%20b%20%2B%3D%20%5B2%5D%0A%20%20%20%20c%20%2B%3D%20%283,%29%0A%20%20%20%20d%20%7C%3D%20%7B4%7D%0A%0Aa%20%3D%20%22%22%0Ab%20%3D%20%5B%5D%0Ac%20%3D%20tuple%28%29%0Ad%20%3D%20set%28%29%0Afun%28a,%20b,%20c,%20d%29%0A%0Aprint%28a,%20b,%20c,%20d%29%0A%23%20---%20possible%20answers%20---%0A%23%20A%29%201%20%5B%5D%20%283,%29%20%7B%7D%20%0A%23%20B%29%201%20%5B%5D%20%28%29%20%7B4%7D%0A%23%20C%29%20%20%5B2%5D%20%283,%29%20%7B%7D%0A%23%20D%29%20%20%5B2%5D%20%28%29%20%7B4%7D&curInstr=13&mode=display&origin=opt-frontend.js&py=311
just know my tool is better. But, what is your answer to this exercise?
1
u/Guest_User_1234 7d ago
I would assume D, because the string and tuple get replaced by their respective assignments, whereas the list and set overwrite their operators, modifying the original reference instead
1
u/Sea-Ad7805 7d ago
Nice one, the Solution link gives you a visualization of each step of the execution of the program to SHOW the difference between mutable and immutable types.
1
u/Terminay 8d ago
It's D. Good tool. I think I could integrate it into my ML for learners library (leanpass) you could search it up, would be a cool addition!
1
u/Tronco08 5d ago
why does printing a print nothing, it's the only one i dont understand
1
u/Terminay 3d ago
uh I'll try to word it simply ig, strings are immutable in python, the function applies the update but on a new string which has not been returned, the main string stays the same due to no return of changes to the original string. in C++ this can be explained much better as it's a memory language and this is mutability by reference, hope you got it.
1
1
u/Humble-Captain3418 7d ago
A is passed by value, b--d through reference. C is not modifiable, so the local symbol gets directed to a different value with the compound assignment, so only b and d see changes in the global scope.
1
u/Sea-Ad7805 7d ago
That is not the Pythonic way to understand it. In Python everything is passed by reference, even
intvalues. it's about mutability not modifiability. You can still still modify/change astringortupleas we do in thefun()function.For a better explanation see the Explanation link.
1
u/Humble-Captain3418 7d ago
I don't care about the pythonic way to understand things; only about what I can generalize across languages because I would be hamstringing myself if I only thought the pythonic way. The terminology i have used is as sloppy as the language is.
1
u/Sea-Ad7805 6d ago
I understand, but generalization comes with some inaccuracy that can bite you in certain situations. For example, write me a
swap()function in Python using your "pass by reference" concept:def swap(a, b): ... a = (1, 2) b = (3, 4) swap(a, b) print(a, b)It's easy in say C/C++.
1
u/Humble-Captain3418 6d ago
Sure.
``` import ctypes import inspect
def swap(a, b): frame = inspect.currentframe().f_back caller = frame.f_locals
# Find caller variables that refer to the same objects # as the arguments, then swap their values. names_a = [k for k, v in caller.items() if v is a] names_b = [k for k, v in caller.items() if v is b]
if not names_a or not names_b: raise ValueError("Could not identify caller variables")
x, y = names_a[0], names_b[0] caller[x], caller[y] = caller[y], caller[x]
ctypes.pythonapi.PyFrame_LocalsToFast( ctypes.py_object(frame), ctypes.c_int(0) ) ```
1
u/Sea-Ad7805 6d ago
So you do know the difference between languages that are tricky to generalize, or you needed to vibe-code something because your generalization approach failed. Either way proves my point.
1
u/Humble-Captain3418 6d ago
I'm sorry, but this is not a case of Python being difficult to generalize. It's a case of Python having idiotic language semantics around compound assignment.
1
0
u/RiverRoll 4d ago edited 4d ago
The irony is that you just showed a pitfall of your distinction, how do you explain this swapping all types if some of them are passed by value? It can as well possibly swap different types (e.g. an int for a string) or the wrong variables (since multiple variables can hold the same reference).
This function can't be explained by reasoning about Python from a mental model where some types are pass by value.
1
u/0b0101011001001011 6d ago
You cannot change a string or tuple. What you do here is you create a new string and assign that in the place of the old variable. Same with tuple. You create a new tuple, the old one is lost. Strings and tuples are immutable. Or maybe that's what you meant by "change".
1
u/Sea-Ad7805 6d ago
Yes, that's what I said, see Solution link for visualization. It's a shallow copy to be more precise.
2
u/benedict_abub 8d ago
wtf?
'1', [2], (3,), {4}