An exercise to help build the right mental model for Python data.
- Solution: https://memory-graph.com/#codeurl=https%3A%2F%2Fraw.githubusercontent.com%2Fbterwijn%2Fmemory_graph_videos%2Frefs%2Fheads%2Fmain%2Fexercises%2Fexercise9.py&play=
- Explanation: https://github.com/bterwijn/memory_graph?tab=readme-ov-file#python-data-model
The “Solution” link visualizes execution and reveals what’s actually happening using 𝗺𝗲𝗺𝗼𝗿𝘆_𝗴𝗿𝗮𝗽𝗵: https://github.com/bterwijn/memory_graph
That’s what you get because you’re afraid of pointers 😁! /j
What the fuck
Hot take: if
a += bis not the same asa = a + b, you done fucked upIt’s definitely not the same. Similarly for a class you can define the
__add__dunder method fora + band separately the__iadd__dunder method fora += b. The first creates a new object, the latter changes/mutates the existing objecta. For immutable types it is the same though.For immutable types it is the same though.
The most twisted thing I learned is that all
ints below a fixed limit share the sameid()result, so>>> x = 1 >>> id(x) 135993914337648 >>> y = 1 >>> id(y) 135993914337648But then suddenly:
>>> x = 1000000 >>> id(x) 135993893250992 >>> y = 1000000 >>> id(y) 135993893251056Using
id()as a key indict()may get you into trouble.Oh absolutely, I understand that the language allows implementations to violate my proposed equivalence — I’m saying that’s a bad implementation (some might say a bad language, for allowing bad implementations, but I don’t necessarily agree)
Tell me again how python is easy to learn for beginner programmers.
Other languages that have similar behavior include Java and JavaScript, and yes you have to be careful with list / array operations in those languages as well, lest you operate on the wrong list inadvertently. Happened to me. It will happen to you.
You don’t have to compile. You don’t need semicolons.
Python was my first programming language, and those two things alone honestly are really nice. Doesn’t mean there aren’t a million other issues and difficulties, though, lol.
I expect A if “b” is a clone, or E if it’s a reference. But I also wouldnt combine array operations like this.
The answer being C feels like a bug.
In my limited understanding, the 5th step
b = b + [4]would cause problems, infinite execution or alike, if it kept being a reference.Coming from MATLAB, anything but A feels like a bug. I don’t want my script to use references when initializing a variable unless I tell it to.
Eh, I get it. The equal operator creates a reference but the plus operator isn’t destructive so it creates a new list and overwrites the variable b with a new list, when assigned.
Of course, this would all be avoided if creating copies was the norm; which is why I stick with functional languages.
Copying a list with a million elements every time you make a small change is not fun. Sure, you can optimize a bit behind the scenes, but that still gives a lot of overhead.
And we can create data structures and algorithms that fit a more functional style without relying on imperative assumptions of how data should be handled. Data structures like vlists could be applicable, for example.




