Python's Data Model Explained through Visualization

An exercise to help build the right mental model for Python data.

The “Solution” link visualizes execution and reveals what’s actually happening using 𝗺𝗲𝗺𝗼𝗿𝘆_𝗴𝗿𝗮𝗽𝗵: https://github.com/bterwijn/memory_graph

33 points · 13 comments · view on lemmy.world

13 Comments

kibiz0r@midwest.social · 19 pts · 142d (3 replies)

Hot take: if a += b is not the same as a = a + b, you done fucked up

bterwijn@programming.dev · 5 pts · 142d (2 replies)

It's definitely not the same. Similarly for a class you can define the __add__ dunder method for a + b and separately the __iadd__ dunder method for a += b. The first creates a new object, the latter changes/mutates the existing object a. For immutable types it is the same though.

JATothrim_v2@programming.dev · 6 pts · 142d

For immutable types it is the same though.

The most twisted thing I learned is that all ints below a fixed limit share the same id() result, so

>>> x = 1
>>> id(x)
135993914337648
>>> y = 1
>>> id(y)
135993914337648

But then suddenly:

>>> x = 1000000
>>> id(x)
135993893250992
>>> y = 1000000
>>> id(y)
135993893251056

Using id() as a key in dict() 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 (4 replies)

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 (2 replies)

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 (1 reply)

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 (1 reply)

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.