Soon after you start to learn #Python, you start to hear that some data is mutable (i.e., can be changed), whereas other data is immutable (i.e., cannot be changed).
I find that many developers confuse "immutable" with "constant." These are very different ideas.
To appreciate the difference, remember that a Python variable is a reference to an object. It is *not* an alias for a location in memory.
So when you say "x = 5", you aren't sticking 5 in x's memory location. Rather, you are saying that the name "x" is another way to refer to 5.
In that sense, variables in Python are sort of like pronouns. You can refer to the object itself, or you can refer to it via its pronoun. However you refer to it, you get the same object.
When you assign a variable, you're saying that it (the pronoun) now refers to a new object.
Assignment breaks the connection between the variable and its previous object, and adds the connect to the new object.
Any variable can refer to any object. And any variable can be reassigned to any object at any time.
Some languages have "constants." You can think of these as variables that can be assigned, but only once. Trying to break the connection with their referred object will result in an error.
Python doesn't have constants. They simply don't exist.
I mean, we kind of pretend they do; if a name is in ALL_CAPS, then we call it a constant. But this is a convention; Python constants can be reassigned just as easily as any other name.
So, what does the mutable/immutable divide have to do with this?
Nothing at all, actually!
Mutable vs. immutable has nothing to do with variables, what names are referring to an object, or whether the language supports constants.
Mutability has to do with whether the data can be changed.
Specifically: If x refers to a list, can the list change without reassigning x?
In Python's case, the answer is "yes." If x and y both refer to the same list, then you can modify the list via either x or y. Both x and y continue to refer to the same list, but the list has changed.
Here's a potentially weird analogy:
Let's say you reconnect with a childhood friend. You haven't seen each other in a long time. They have the same name, and are still the same person. But they have changed since you last saw them.
Same object, same name, different contents.
Strings are immutable, which means that most string methods return a new string. They can't modify the existing one.
Lists are mutable, which means that many methods (e.g., append) can modify the list without returning a new one. Anyone referring to the list sees the change.
One final point: Integers are immutable, but we don't make a big deal out of it. I mean, you likely don't want the value of 5 to suddenly change to 6. Rather, you would want to assign your variable to 6.
That's intuitive, but immutable strings are less so.
Bottom line, immutable ≠ constant.
I hope that this makes things clearer. I know that it's a topic that many people struggle with when they're new to Python.
• • •
Missing some Tweet in this thread? You can try to
force a refresh
Background: I've done corporate #Python and data-science training for 20 years. Even before the pandemic, I taught live, online courses (via WebEx and Zoom) at least 1 week/month. I also offer many video (recorded) courses.
My work slowed down in April-May, when companies didn't know what was happening.
Training is now about where it was before. Except it's 100% online.
I teach everything from "Python for non-programmers" to "intro to data science." 5 days/week, 4-8 hours/day. All online.
I've learned a lot in this time, and want to share these thoughts with others — learners (no pun intended), teachers, and training managers.
Also: I teach adults at companies. I have huge respect and sympathy for schoolteachers who have been thrust into this world.