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?
Your guess was spot on! The tuple and the dictionaries are stored in different memory locations — different shelves in the storage room, if you prefer. So one structure contains the reference for where to find "its contents", which are stored elsewhere in the storage room
But I'll summarise here: each object created has a reference count, so it stores within it the number of references there are to it in the program. So if your create a list and assign to a name:
`my_numbers = [3, 5, 6]
the object created, of type list, will contain the values of course, but also a reference count of 1. There's only the name `my_numbers` referring to it.
But then, you write `lucky_numbers = my_numbers` and the same object now has two references, so ref count becomes 2.
You may then add `important_stuff = {"top priority": my_numbers}` - even though you didn't assign a new name to the list, you get refer to it using `important_stuff["top priority"]`, so ref count becomes 3, and so on.
You can also remove references by removing the list from the dictionary above, for example, reassigning a name to another object (as in the original example you asked about), or using the `del` keyword which deletes a name (not the object)
When an object's reference count reaches 0, you no longer have a way of accessing that object, therefore the garbage collector will take care of it soon.
I just keep learning stuff today. So if del deletes the keyword, not the object, calling del is kind of superfluous, unless the point is just to make sure that nothing can access that value before the function ends
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?
Your guess was spot on! The tuple and the dictionaries are stored in different memory locations — different shelves in the storage room, if you prefer. So one structure contains the reference for where to find "its contents", which are stored elsewhere in the storage room
Your second question is a topic I'm planning to write about very soon. Are you on Twitter / X, if you are I had this thread there a long while ago: https://x.com/s_gruppetta_ct/status/1606257500159766529?s=20
But I'll summarise here: each object created has a reference count, so it stores within it the number of references there are to it in the program. So if your create a list and assign to a name:
`my_numbers = [3, 5, 6]
the object created, of type list, will contain the values of course, but also a reference count of 1. There's only the name `my_numbers` referring to it.
But then, you write `lucky_numbers = my_numbers` and the same object now has two references, so ref count becomes 2.
You may then add `important_stuff = {"top priority": my_numbers}` - even though you didn't assign a new name to the list, you get refer to it using `important_stuff["top priority"]`, so ref count becomes 3, and so on.
You can also remove references by removing the list from the dictionary above, for example, reassigning a name to another object (as in the original example you asked about), or using the `del` keyword which deletes a name (not the object)
When an object's reference count reaches 0, you no longer have a way of accessing that object, therefore the garbage collector will take care of it soon.
I just keep learning stuff today. So if del deletes the keyword, not the object, calling del is kind of superfluous, unless the point is just to make sure that nothing can access that value before the function ends
If the name you’re deleting is the last remaining reference to the object, then the object will be deleted from memory too