11 Comments

This was great - I definitely predicted the title wrong haha. I also recently learned that simultaneous assignment is not perfectly simultaneous!! Keep it up Stephen

Expand full comment
Jan 19Liked by Stephen Gruppetta

I learned a lot with this one. When you started with talking about being able to change the values in a dictionary in a tuple, my first guess for why was *close*. I figured that the values of the tuples were the dictionary, so as long as the dictionary is still there, the tuple hasn't changed, no matter if changes are made inside the dictionary. But it makes sense that it's not the actual object in the tuple but a reference to it.

But I have a question about reassigning the variable name of a string (or any other immutable type). When you assign the new string to the old string's variable name, what happens to the old string? Is it gone, removed from memory or just hanging out, unreachable until garbage collection?

Expand full comment

Fun fact: At least in CPython, strings are technically not immutable (although for all practical purposes they are). This is due to an optimization made for string addition.

If you do a:

string = string + stringtwo

and the reference count of string before the operation is one, then CPython reuses string instead of allocating a new memory location. You can check this by doing id(string) before and after the operation.

Expand full comment