Immutability Quirks and Oddities in Python
This is the last lesson of the course and covers quirks and oddities arising when using immutable objects. One of these oddities is a (immutable) tuple containing a (mutable) list. This example is covered in more depth in the video.
Congratulations, you made it to the end of the course! What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment in the discussion section and let us know.
00:00 What I want to do now is I want to talk about some of the quirks when it comes to immutability in Python. So what I’m going to do now is I’m going to create a tuple, which is immutable, and I’m going to put a list into it, which is mutable.
00:13 And we’re going to encounter a pretty interesting situation here—or, well, I guess almost a paradox, depending on what definition of mutable and immutable you use.
00:24
So I’m going to place some values into this tuple. And at index 1
,
00:29 I am going to add a list.
00:36
Okay, so now we’ve looked at this tuple. We’ve got the tuple here, it starts with the value 1
, and then we’ve got this list which sits at index 1
.
00:47
And then we’ve got a string here at index 2
in the tuple. Now, if I try and modify this tuple and let’s say we want to try and modify the first value. We can’t do it, right?
00:58
Because the tuple is immutable. The same thing is true when I try to, let’s say, change this string here, 'word'
, change it and make it uppercase. If I wanted to do that, that doesn’t work because strings are immutable too, right? Now, you can already see here, the TypeError
didn’t say “Hey, you can’t do that because the tuple is immutable, or does not support item assignment.” It said the 'str' object does not support item assignment
.
01:34
So, can you guess what’s going to happen when I try to modify the list at index 1
? Let’s say we’re going to do the same thing and I’m going to modify this list.
01:48 And so the surprising result here is that this actually worked. We modified the tuple—or, it depends on your definition, right? Python says we didn’t modify the tuple. All the tuple cares about is, well, it points to some values and one of them is a list, and you can’t change what lists this tuple is pointing to. But because a list is mutable, you can very well reach in and modify the list.
02:16 So in a way, Python’s definition of mutability and immutability is non-transitive. So it means if the tuple is immutable, that doesn’t guarantee that an object referenced by the tuple is also immutable, or that all objects referenced by the tuple are also immutable.
02:34 So, the mutability—it kind of stops after the first level, right? That guarantee stops after the first level. So we can’t modify the tuple itself, but we can reach in and modify objects that the tuple holds.
02:48 This can be a little bit confusing when you see it the first time or when you just hear the word immutable. And I’ve seen this over and over again, working with people and also people commenting on my blog posts and tutorials.
03:00 It is just a point of confusion for new Python programmers, so that’s why I decided to do this video. What you can see here is we were able to modify this object even though it is immutable.
03:10 This all comes down to Python’s definition of immutability, and the fact that immutability in Python is not transitive right? It stops after the first level.
03:20 There’s no guarantees that further down in the object hierarchy there isn’t a mutable object. So, that’s something to keep in mind, and that’s what I meant earlier that sometimes in Python it can be a little bit hard to actually write code and use data structures that are always immutable.
03:35 You really have to be careful not to introduce something like this, where you have an immutable data structure that then contains a mutable data structure, and that means it can be freely modified and there’s no immutability guaranteed, because immutability is non-transitive. All right.
03:51 So, not everyone is going to encounter this and I don’t want to discourage you with this video and confuse you. But if you do encounter it, then you’re going to see why it happens and you’re going to understand why this happens.
04:04 So, this is just something to be aware of when it comes to immutability and mutability in Python. All right, so I hope this video helped you out. Talk soon and happy Pythoning!
RD on April 19, 2019
Hi, To answer your question lets first understand what happens when you create a string variable: str_var = ‘abc’
Python creates an object ‘abc’ in memory, lets say at location 1000 and assigns this object’s reference to str_var variable (= sign) Now in the next line of your code when you write: str_var = ‘xyz’
Python creates a new object ‘xyz’, lets say at location 2000 and then assigns it to the str_var.
So we didnt change the ‘abc’ string object. We changed which object the variable str_var is referencing.
Additionally, If you are wondering what happened to the object ‘abc’ , it will be mopped up by garbage collection unless any other variable is referencing it.
chmayurnath on April 24, 2019
Thanks Understood.
Abby Jones on June 28, 2019
This really is quirky but I dig it. Being able to access a mutable object within an immutable object.
Tonya Sims on July 3, 2019
Wow! This video series cleared up so much confusion for me regarding immutable objects! Thank you Dan!
anders stenborg martin on July 15, 2020
Very clear and concise!
Ghani on Oct. 17, 2020
Very interesting; thank you so much!
VitaminC on July 17, 2021
Great course - concise and informative. Cheers.
h602421365 on Dec. 25, 2021
What about the other way arround? instead of a list in a tuple, a tuple in a list? Can you modify the tuple in the list?
Bartosz Zaczyński RP Team on Jan. 3, 2022
@h602421365 Have you tried? Spoiler alert: you can’t modify a tuple once you created one, so it wouldn’t work.
Otto Gyuris on Feb. 9, 2022
Great summary! Learned it. Thanks! :)
Become a Member to join the conversation.
chmayurnath on April 18, 2019
Hi,
I have a doubt on immutability, might be basic, but please clarify .
if I re assign a value to string or tuple variable, it does get changed, Please let me know why
eg String str=”abc” str=”xyz”
if we print str it would be xyz. why str is getting modified if we reassign a complete value.
Thanks