this post was submitted on 01 Apr 2026
25 points (90.3% liked)

Python

7863 readers
1 users here now

Welcome to the Python community on the programming.dev Lemmy instance!

๐Ÿ“… Events

PastNovember 2023

October 2023

July 2023

August 2023

September 2023

๐Ÿ Python project:
๐Ÿ’“ Python Community:
โœจ Python Ecosystem:
๐ŸŒŒ Fediverse
Communities
Projects
Feeds

founded 2 years ago
MODERATORS
 

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

you are viewing a single comment's thread
view the rest of the comments
[โ€“] Oka@sopuli.xyz 4 points 1 week ago* (last edited 1 week ago) (2 children)

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.

[โ€“] tomenzgg@midwest.social 3 points 1 week ago (1 children)

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 0 points 1 week ago (1 children)

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 1 points 1 week ago

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.