One of the quietest bugs you’ll ever hit has nothing to do with logic or syntax. In fact, it comes from how data behaves behind the scenes.

It all ties to this concept that’s shared across all programming languages: mutability.

Defining mutability (and immutability)

Mutability refers to whether or not an object’s value can be changed after it’s created in memory. To summarize the difference:

  • Mutable objects can be changed in place; you can add, remove, or alter their contents without creating a new object.

  • Immutable objects can’t be changed once created. Any “change” isn’t truly a change, it’s a creation of a new object.

This concept is better demonstrated with an example. Take the following:

def add_item(cart: list) -> None:
     cart.append("apple")

my_cart = ["banana"]
add_item(my_cart)

print(my_cart)

One thing to point out specifically is that there is no return value in the add_item function.

Because lists are mutable objects, the variable my_cart becomes [“banana”, “apple”] even though we never returned the cart variable. This can make debugging sessions an absolute nightmare.

If we were to use a tuple instead of a list, we’d modify the code to create a new tuple instead of modifying the original one:

def add_item(cart: tuple) -> tuple:
     return cart + ("apple",)

my_cart = ("banana",)
my_cart = add_item(my_cart)

print(my_cart)

It does look like we’re updating the cart’s values, but what’s really going on under the hood is a new tuple is being created and assigned to the variable.

This is to say that mutable objects are bad, because they aren’t. There’s times when you’d use them and when you wouldn’t.

Tradeoffs of using mutable vs immutable

You’re going to want to use a mutable data type when you’re adding/removing/changing things often.

On the contrary, you’re going to want to use an immutable data type when you want the data to stay the same.

With the examples provided, it would make more sense to use a mutable data type since we can always add/remove items from the cart. Later down the line (i.e. checkout) we may want to use a tuple since we don’t want things updating.

Mutable vs Immutable data types listing

Below is a list of some of the more common data types that are built into Python that are mutable and immutable for your reference.

Type

Mutable?

Integer

Float

Boolean

String

List

Dictionary

Set

Tuple

Bytes

Happy coding!

📧 Join the Python Snacks Newsletter! 🐍

Want even more Python-related content that’s useful? Here’s 3 reasons why you should subscribe the Python Snacks newsletter:

  1. Get Ahead in Python with bite-sized Python tips and tricks delivered straight to your inbox, like the one above.

  2. Exclusive Subscriber Perks: Receive a curated selection of up to 6 high-impact Python resources, tips, and exclusive insights with each email.

  3. Get Smarter with Python in under 5 minutes. Your next Python breakthrough could just an email away.

You can unsubscribe at any time.

Interested in starting a newsletter or a blog?

Do you have a wealth of knowledge and insights to share with the world? Starting your own newsletter or blog is an excellent way to establish yourself as an authority in your field, connect with a like-minded community, and open up new opportunities.

If TikTok, Twitter, Facebook, or other social media platforms were to get banned, you’d lose all your followers. This is why you should start a newsletter: you own your audience.

This article may contain affiliate links. Affiliate links come at no cost to you and support the costs of this blog. Should you purchase a product/service from an affiliate link, it will come at no additional cost to you.

Reply

Avatar

or to participate

Keep Reading

No posts found