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 must log in or register to comment.
It’s always fun to guess whether python will call
__add__or__iadd__.__add__is called with+and__iadd__is called with+=, and there is a difference: https://www.reddit.com/r/PythonLearning/comments/1nw08wu/right_mental_model_for_python_data/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?
I so wonder what a will result in.
Yes
ayou get for free to set the tone, the others are more interesting.

