Join Barron Stone for an in-depth discussion in this video Mutable and immutable data, part of Programming Foundations: Real-World Examples.
- As you learn about programming, you might encounter two funny sounding words: mutable and immutable, which are just fancy words to describe whether or not something can be changed. A mutable object can be modified after it's been created, whereas an immutable object cannot. Once you've created an immutable object, you're stuck with it. Object types will either be mutable or immutable, and it's a good idea to learn which is which in your programming language, so you can be sure to select the right type of object for different jobs. For example, you'd want to avoid using immutable data types to store values in your program that we're going to constantly be changing or needing to be changed.
One example of a mutable object in Python is the list, which is an object type that acts as a container to store an ordered collection of other objects. You can think of this rack of clothes as a list, which contains several different clothing objects. As Olivia sorts through the rack of clothes to pick an outfit for today, she can change the contents of the list by removing items or putting them back in a different position, because the list is a mutable data type. Every time she inserts or removes an item from the rack, the value of that list object changes, but the existence of the list is unaffected .
Olivia's always interacting with the same rack of clothes. Now, let's consider an immutable data type that cannot be changed; a string. Since a string of words in Python is immutable, you could think of like a series of words that you've spoken out loud. - What do you think of this outfit? - You're wearing that? Is not what I should have said to my wife, but I did say it, and unfortunately, I cannot change those words because they're immutable. When I use a function or method on a Python string object, I'm not actually changing the string.
Those words have already been spoken. All I can do is create a new string object with a smarter choice of words, and use that to replace the old string. What I meant to say was, "You're wearing that because you look great!" And I think this is a good time to look at some code. To demonstrate mutable and immutable objects, I'm going to be creating a list and some strings using the Python interactive Shell. You can see here that I've already created a list object named "closet," which contains several strings for various clothing items.
If I type "closet," and hit Enter, the Python Shell will print out the contents of the closet list. You can see that it currently contains a shirt, hat, pants, jacket, and socks, in that order. I can check the ID of the list object, and see that it returns a unique ID that Python has assigned to this list. Now, let's make some changes to the closet. I can remove items from the list by using the remove method, and I'll use that now to take out the hat. If I print out the contents of the closet again, you can see that the value of the list is different now, because it no longer contains the hat, but if I check the list ID again with the ID function, you can see that it has the exact same ID as before.
The name "closet" points to the exact same list object as before. I've just changed the value of that list object, because it's mutable. Now, I'll switch gears to look at an immutable Python datatype: strings. I'll create a new string object named "words," which contains my poor choice of words from earlier. "You're wearing that?" If I type "words," the shell will print out the contents of that string object, and we can use the ID function on it to see that object's unique ID, but those words were not a good choice of words for me to say to Olivia, so I'll try modifying that string object to make it a more positive statement by concatenating the phrase, "because you look great!" Now, if I look at the words object, I'll see that it contains the entire phrase, "You're wearing that because you look great," which is a better choice of words than we originally had.
However, if I check the ID for that object, it's different than the previous ID for words. This is an important thing to realize about immutable objects. When I used the plus operator to append a phrase to the end of words, it did not actually modify the original string object, because that can't be changed. Strings are immutable. What happened here is that the plus operator created a new string object containing the combined phrases. Then the equals operator bound that new object to the named "words," effectively replacing the old string.
Keep this in mind whenever you're working with immutable types. Every time I use one of the string methods to modify the contents of a string, Python is actually having to create an entirely new string object in the background.
- Reusing functions
- Local vs. global variables
- Creating and naming custom objects
- Class inheritance
- Modules and packages
- Multidimensional lists and tuples
- Queues and stacks
- Creating and combining sets
- Storing data in dictionaries
- If/else and switch statements
- For vs. while loops
- Error handling
- Polling and event-driven programming