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

13 Comments
kibiz0r@midwest.social · 19 pts · 142d
Hot take: if
a += bis not the same asa = a + b, you done fucked upbterwijn@programming.dev · 5 pts · 142d
It'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.JATothrim_v2@programming.dev · 6 pts · 142d
The most twisted thing I learned is that all
ints below a fixed limit share the sameid()result, soBut then suddenly:
Using
id()as a key indict()may get you into trouble.kibiz0r@midwest.social · 2 pts · 142d
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)
Valmond@lemmy.dbzer0.com · 16 pts · 142d
That's what you get because you're afraid of pointers 😁! /j
zo0@programming.dev · 11 pts · 142d
What the fuck
Oka@sopuli.xyz · 5 pts · 142d
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.
Successful_Try543@feddit.org · 4 pts · 141d
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.
tomenzgg@midwest.social · 2 pts · 142d
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.
bterwijn@programming.dev · 2 pts · 141d
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.
tomenzgg@midwest.social · 2 pts · 141d
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.
aev_software@programming.dev · 5 pts · 142d
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.
vonbaronhans@midwest.social · 2 pts · 142d
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.