A hard 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%2Fexercise26.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


Like anything else, these sorts if issues are rather murky and are directly impacted by the user’s competence in the tools.
Does python have obvious overhead issues? Yes.
Does Python have to be super inefficient? No.
Take basic set operations, for example. Do it manually in the language and it will be dog slow. Do it using the set class? You are leveraging the speed of the underlying C implementation.
E.g., In competitive programming eventually need C or Java, but a strong Python user can move the bar significantly in terms of how many problems are possible with Python.
I don’t believe this to be the case. The immutability is precisely why efficient structural sharing is possible without screwing up other data structures. And for standard stuff, it is actually happening behind the scenes already.
You see similar claims about recursion in general, but those claims are often so broad that they don’t hold water. I mean, yea, if your language sucks at optimizing recursion it isn’t going to be a pleasant experience, but tail call elimination, lazy evaluation, etc. mean you can write some really awesome and efficient code.
I think the bigger problem is that it takes a lot of time to internalize what is going on under the hood when you make a call or initialize a data structure.
I’ve built a nice visualizer for internalizing what is going on under the hood, I hope that can help people.
Well, that is interesting, isn’t it?
Thank you for the lead. I’m setting that link aside. I might use it in class next time.
Great, I hope it can bring much value for your teaching.