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
 

Keep Current with Reuven M. Lerner

Reuven M. Lerner Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @reuvenmlerner

2 Jun
One of the hardest things for people to learn in #Python is list comprehensions. Some quick tips that make them easier to work with:

(1) Break them up into multiple lines! It drives me batty to see people writing comprehensions on a single line.
You can then reason about each line separately:

[int(x)
for x in '1 2 b 3'.split()
if x is.digit()]

Line 1: Expression
Line 2: Iteration
Line 3: Condition

Or if you're a fan of SQL:

Line 1: SELECT
Line 2: FROM
Line 3: WHERE
(2) The expression can be literally any Python expression. Any operator, function, or method. Including functions that you write.

(3) Don't use print as an expression. Comprehensions create lists. Print displays data on the screen. Also, print returns None — not what you want.
Read 12 tweets
15 Oct 20
Some thoughts on teaching online (a thread).

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.
Read 20 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!

:(