this post was submitted on 23 Oct 2025
20 points (85.7% liked)

Python

7589 readers
6 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 uses memory_graph to visualize execution and reveals what’s actually happening:

you are viewing a single comment's thread
view the rest of the comments
[–] joyjoy@lemmy.zip 3 points 1 month ago* (last edited 1 month ago) (1 children)

Except if __iadd__ doesn't exist on the type, then __add__ is called. The variable is always reassigned. Quick example:

x = (0, [1, 2])
try:
    x[1] += [3]  # calls list.__iadd__, then reassigns x[1]
except TypeError as e:
    print(e)

x += (4,)  # calls tuple.__add__, then reassigns x

print(x)

What is printed?